﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using MS.Core.Base.Transaction;
using System.Collections.Specialized;
using LHS.Core.TableInfo;
using MS.Core.Constants.Objects;
using LHS.Win.Report;
using System.Drawing.Printing;
using System.IO;


namespace LHS.Win
{
    public partial class frmHoSoBenhAn : Form
    {
        #region Variable
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        private bool _blnInsertSurgeryDetail = false;
        private bool _blnChangeImage = false;
        #endregion
        // Methods
        public frmHoSoBenhAn(string clrISN, string _NhanBiet, string PatISN)
        {
            this.InitializeComponent();
            NhanBiet = _NhanBiet;
            this.clrISN1 = clrISN;
            this.patISN = PatISN;
        }

        #region Context Menu Strip Event

        private void AddLoiKhuyen(object sender, EventArgs e)
        {
            if (this.tbLoiKhuyen.Text == "")
            {
                this.tbLoiKhuyen.Text = "- " + sender.ToString().Substring(0x35);
            }
            else
            {
                this.tbLoiKhuyen.Text = this.tbLoiKhuyen.Text + "\r\n- " + sender.ToString().Substring(0x35);
            }
            Point pos = new Point(13, 0x18);
            this.ctmLoiKhuyen.Show(this.tbLoiKhuyen, pos);
        }

        private void AddPhauThuat(object sender, EventArgs e)
        {
            if (gdPT.DataSource == null)
            {
                DataTable dtNew = new DataTable();
                dtNew.Columns.Add(InfoSurgical.SURNAME, typeof(string));
                dtNew.Columns.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY, typeof(int));
                gdPT.DataSource = dtNew;
            }
            DataTable dt = (DataTable)gdPT.DataSource;

            if (dt.Select(InfoSurgical.SURNAME + "='" + sender.ToString() + "'").Length == 0)
            {
                DataRow dr = dt.NewRow();
                dr[InfoSurgical.SURNAME] = sender.ToString();
                dt.Rows.Add(dr);
            }
            Point pos = new Point(12, 0x18);
            this.ctmPhauThuat.Show(this.gdPT, pos);
        }

        private void AddThuThuat(object sender, EventArgs e)
        {
            if (this.tbThuThuat.Text == "")
            {
                this.tbThuThuat.Text = "- " + sender.ToString().Substring(0x35);
            }
            else
            {
                this.tbThuThuat.Text = this.tbThuThuat.Text + "\r\n- " + sender.ToString().Substring(0x35);
            }
            Point pos = new Point(13, 0x18);
            this.ctmThuThuat.Show(this.tbThuThuat, pos);
        }

        /// <summary>
        /// Add thong tin tien ich cho Tien can, Dien Bien Benh, Nghe Nghiep, Benh Su
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddTienCan(object sender, EventArgs e)
        {
            if (this.tbTienCan.Text == "")
            {
                this.tbTienCan.Text = "- " + sender.ToString();
            }
            else
            {
                this.tbTienCan.Text = this.tbTienCan.Text + "\r\n- " + sender.ToString();
            }
            Point pos = new Point(12, 0x18);
            ctmTienCan.Show(tbTienCan, pos);
        }

        private void AddBenhSu(object sender, EventArgs e)
        {
            if (this.tbBSu.Text == "")
            {
                this.tbBSu.Text = "- " + sender.ToString();
            }
            else
            {
                this.tbBSu.Text = this.tbBSu.Text + "\r\n- " + sender.ToString();
            }
            Point pos = new Point(12, 0x18);

            ctmBenhSu.Show(this.tbBSu, pos);
        }

        private void AddDienBienBenh(object sender, EventArgs e)
        {
            if (this.tbDienBien.Text == "")
            {
                this.tbDienBien.Text = "- " + sender.ToString();
            }
            else
            {
                this.tbDienBien.Text = this.tbDienBien.Text + "\r\n- " + sender.ToString();
            }
            Point pos = new Point(12, 0x18);
            ctmDienBienBenh.Show(this.tbDienBien, pos);
        }

        private void AddNgheNghiep(object sender, EventArgs e)
        {
            this.tbJob.Text = sender.ToString();
            Point pos = new Point(12, 0x18);
            ctmNgheNghiep.Show(this.tbJob, pos);
        }
        #endregion

        #region Button Event
        private void btAdd_Click(object sender, EventArgs e)
        {
            this.treeView1_DoubleClick(sender, e);
        }

        private void btAddChanDoan_Click(object sender, EventArgs e)
        {
            this.treeView2_DoubleClick(sender, e);
        }

        private void btAddPic_Click(object sender, EventArgs e)
        {
            if (this.fLapHinhAnh == null)
            {
                this.fLapHinhAnh = new frmLapHinhAnh(this);
                this.fLapHinhAnh.Show();
            }
            if (this.fLapHinhAnh.flag == 0)
            {
                this.fLapHinhAnh = new frmLapHinhAnh(this);
                this.fLapHinhAnh.Show();
            }
            if (this.fLapHinhAnh.flag == 1)
            {
                this.fLapHinhAnh.Activate();
            }
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            if (this.checkData())
            {
                try
                {
                    if ((this.tbPhone.Text.Length > 0) && !this.IsNumber(this.tbPhone.Text.ToString()))
                    {
                        MessageBox.Show("Số điện thoại không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbDiDong.Text.Length > 0) && !this.IsNumber(this.tbDiDong.Text.ToString()))
                    {
                        MessageBox.Show("Số điện thoại di động không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbMach.Text.Length > 0) && !this.IsNumber(this.tbMach.Text.ToString()))
                    {
                        MessageBox.Show("Mạch không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbNhietDo.Text.Length > 0) && !this.IsFloat(this.tbNhietDo.Text.ToString()))
                    {
                        MessageBox.Show("Nhiệt độ không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbCanNang.Text.Length > 0) && !this.IsFloat(this.tbCanNang.Text.ToString()))
                    {
                        MessageBox.Show("Cân nặng không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        string str;
                        string str2;
                        string str3;
                        string str4;
                        string str5;
                        string str6;
                        if (this.chkHetBenh.Checked)
                        {
                            this.listView1.Items.Clear();
                            this.lbCD.Items.Clear();
                            this.tbBenhNgoaiDS.Text = "";
                            this.tbCDNgoaiDS.Text = "";
                        }
                        else
                        {
                            if ((this.listView1.Items.Count <= 0) && (this.tbBenhNgoaiDS.Text.Trim() == ""))
                            {
                                MessageBox.Show("Vui lòng chọn triệu chứng!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                return;
                            }
                            if ((this.lbCD.Items.Count <= 0) && (this.tbCDNgoaiDS.Text.Trim() == ""))
                            {
                                MessageBox.Show("Chẩn đoán không hợp lệ!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                return;
                            }
                        }
                        if (this.tbMach.Text == "")
                        {
                            str = "0";
                        }
                        else
                        {
                            str = this.tbMach.Text.ToString();
                        }
                        if (this.tbNhietDo.Text == "")
                        {
                            str2 = "0";
                        }
                        else
                        {
                            str2 = this.tbNhietDo.Text.ToString();
                        }
                        if (this.tbHuyetAp.Text == "")
                        {
                            str3 = "0";
                        }
                        else
                        {
                            str3 = this.tbHuyetAp.Text.ToString();
                        }
                        if (this.tbCanNang.Text == "")
                        {
                            str4 = "0";
                        }
                        else
                        {
                            str4 = this.tbCanNang.Text.ToString();
                        }

                        ///phau thuat 20090822
                        ///cong tat ca cac phau thuat ma user dang chon
                        string strTK4PT = string.Empty;
                        str5 = getStringPhauThuat(ref strTK4PT);

                        if (this.rbMale.Checked)
                        {
                            str6 = "Nam";
                        }
                        else
                        {
                            str6 = "Nữ";
                        }

                        string text = string.Empty;
                        if (!this.cboTaiKham.Visible)
                        {
                            text = "0";
                        }
                        else
                        {
                            text = this.cboTaiKham.Text;
                        }

                        TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                        try
                        {
                            trans.BeginTransaction();
                            //cap nhat benh an + cap nhat benh nhan
                            ListDictionary param = new ListDictionary();
                            param.Add(InfoPatients.PATISN, this.patISN);
                            param.Add(InfoPatients.PATID, this.tbID.Text);
                            param.Add(InfoPatients.PATNAME, this.tbFullName.Text);
                            param.Add(InfoPatients.PATBIRTHDATE, this.dtpBirthDay.Value);
                            param.Add(InfoPatients.PATAGE, this.tbTuoi.Text);
                            param.Add(InfoPatients.PATSEX, str6);
                            param.Add(InfoPatients.PATADDRESS, this.tbAddress.Text);
                            param.Add(InfoPatients.PATTEL, this.tbPhone.Text);
                            param.Add(InfoPatients.PATOCCUPATION, this.tbJob.Text);
                            param.Add(InfoPatients.PATMOBILE, this.tbDiDong.Text);

                            //save Pic Of Patient 20091017
                            int intPatHasPic = savePicOfPatient();
                            param.Add(InfoPatients.PATHASPIC, intPatHasPic);// edit here 20091017

                            param.Add(InfoClinicalRecord.EMPISN, this.cbBacSi.SelectedValue.ToString());
                            param.Add(InfoClinicalRecord.CLRPULSE, str);
                            param.Add(InfoClinicalRecord.CLRTEMPERATURE, str2);
                            param.Add(InfoClinicalRecord.CLRBLOODPRESSURE, str3);
                            param.Add(InfoClinicalRecord.CLRWEIGH, str4);
                            param.Add(InfoClinicalRecord.CLRMEDICALHISTORY, this.tbTienCan.Text);
                            param.Add(InfoClinicalRecord.CLRTEST, string.Empty);
                            param.Add(InfoClinicalRecord.CLRSICKNOTINLIST, this.tbBenhNgoaiDS.Text);
                            param.Add(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text);
                            param.Add(InfoClinicalRecord.CLRSKILL, this.tbThuThuat.Text);
                            param.Add(InfoClinicalRecord.CLRMEDICALADVICE, this.tbLoiKhuyen.Text.ToString());
                            param.Add(InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE, this.dtpNgayTaiKham.Value);
                            param.Add(InfoClinicalRecord.CLRSURGERY, str5);
                            param.Add(InfoClinicalRecord.CLRAPPOINTMENTDATE, this.dtpNgayHen.Value);
                            param.Add(InfoClinicalRecord.CLRSICKCHANGE, this.tbDienBien.Text);
                            param.Add(InfoClinicalRecord.CLRSICKHISTORY, this.tbBSu.Text);
                            param.Add(InfoClinicalRecord.CLRREXAMINATIONTIMES, text);
                            param.Add(InfoClinicalRecord.CLRTYPE, rbTMH.Checked == true ? "1" : "2");
                            param.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY, strTK4PT); //edit here 20090822
                            objController.Exec_StoreProc("sp_ClinicalRecord_Update", param);

                            //tim kiem thong tin
                            List<ExpressionObject> paramGet = new List<ExpressionObject>();
                            paramGet.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));
                            paramGet.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
                            paramGet.Add(new ExpressionObject(InfoClinicalRecord.CLRREXAMINATIONTIMES, this.cboTaiKham.Text));
                            DataTable dtClinicalRecord = objController.GetData("vw_SearchClinicalRecord", paramGet);
                            if (dtClinicalRecord != null && dtClinicalRecord.Rows.Count > 0)
                            {
                                this.clrNoiSoiISN = dtClinicalRecord.Rows[0][InfoClinicalRecord.CLRISN].ToString(); // kiem tra lai o day CLRISN = 0x11

                                ArrayList list = new ArrayList();
                                ArrayList list2 = new ArrayList();
                                ArrayList list3 = new ArrayList();
                                int count = this.listView1.Items.Count;
                                for (int i = 0; i < count; i++)
                                {
                                    string str8 = this.listView1.Items[i].Text.ToString();
                                    switch (this.listView1.Items[i].ForeColor.Name.ToString())
                                    {
                                        case "Red":
                                            list3.Add(0);
                                            break;

                                        case "Blue":
                                            list3.Add(1);
                                            break;

                                        case "Purple":
                                            list3.Add(2);
                                            break;

                                        case "Green":
                                            list3.Add(3);
                                            break;

                                        case "Gray":
                                            list3.Add(4);
                                            break;
                                    }
                                    list.Add(str8);
                                }

                                DataTable dtSick;
                                for (int j = 0; j < list.Count; j++)
                                {
                                    List<ExpressionObject> paramSick = new List<ExpressionObject>();
                                    paramSick.Add(new ExpressionObject(InfoSicks.SCKNAME, list[j].ToString()));
                                    dtSick = objController.GetData(InfoSicks.TABLENAME, paramSick);

                                    list2.Add(dtSick.Rows[0][0].ToString());
                                }

                                //xoa ClinicalSick theo CLRISN
                                List<ExpressionObject> paramDelClinicalSick = new List<ExpressionObject>();
                                paramDelClinicalSick.Add(new ExpressionObject(InfoClinicalSick.CLRISN, this.clrNoiSoiISN));
                                objController.DeleteRow(InfoClinicalSick.TABLENAME, paramDelClinicalSick);

                                DataTable dtClinicalSick = InfoClinicalSick.GetStructure();
                                DataRow drClinicalSick;
                                for (int k = 0; k < list2.Count; k++)
                                {
                                    drClinicalSick = dtClinicalSick.NewRow();
                                    drClinicalSick[InfoClinicalSick.SCKISN] = list2[k].ToString();
                                    drClinicalSick[InfoClinicalSick.CLRISN] = this.clrNoiSoiISN;
                                    drClinicalSick[InfoClinicalSick.SCKSICKLEVEL] = list3[k].ToString();
                                    dtClinicalSick.Rows.Add(drClinicalSick);
                                    objController.InsertRow(drClinicalSick, new string[] { });
                                }

                                //xoa trong PathologicalClinical theo clrISN
                                List<ExpressionObject> paramDelPathologicalClinical = new List<ExpressionObject>();
                                paramDelPathologicalClinical.Add(new ExpressionObject(InfoPathologicalClinical.CLRISN, this.clrNoiSoiISN));
                                objController.DeleteRow(InfoPathologicalClinical.TABLENAME, paramDelPathologicalClinical);

                                //PathologicalOutList
                                List<ExpressionObject> paramGetPathologicalOutList = new List<ExpressionObject>();
                                paramGetPathologicalOutList.Add(new ExpressionObject(InfoPathologicalOutList.CLRISN, this.clrNoiSoiISN));
                                DataTable dtPathologicalOutList = objController.GetData(InfoPathologicalOutList.TABLENAME, paramGetPathologicalOutList);

                                if (dtPathologicalOutList == null || dtPathologicalOutList.Rows.Count == 0)
                                {
                                    ListDictionary paramInsPathologicalOutList = new ListDictionary();
                                    paramInsPathologicalOutList.Add(InfoPathologicalOutList.CLRISN, this.clrNoiSoiISN);
                                    paramInsPathologicalOutList.Add(InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST, this.tbCDNgoaiDS.Text.ToString());
                                    objController.Exec_StoreProc("sp_PathologicalOutList_Insert", paramInsPathologicalOutList);
                                }

                                ArrayList list4 = new ArrayList();
                                ArrayList list5 = new ArrayList();
                                int num5 = this.lbCD.Items.Count;
                                for (int m = 0; m < num5; m++)
                                {
                                    list4.Add(this.lbCD.Items[m].ToString());
                                }

                                //PathologicalSigns
                                DataTable dtPathologicalSigns;
                                for (int n = 0; n < list4.Count; n++)
                                {
                                    List<ExpressionObject> paramPathologicalSigns = new List<ExpressionObject>();
                                    paramPathologicalSigns.Add(new ExpressionObject(InfoPathologicalSigns.PLSPATHOLOGICALSIGNS, list4[n].ToString()));
                                    dtPathologicalSigns = objController.GetData(InfoPathologicalSigns.TABLENAME, paramPathologicalSigns);

                                    list5.Add(dtPathologicalSigns.Rows[0][0].ToString());
                                }

                                DataTable dtPathologicalClinical = InfoPathologicalClinical.GetStructure();
                                DataRow drPathologicalClinical;
                                for (int num8 = 0; num8 < list5.Count; num8++)
                                {
                                    drPathologicalClinical = dtPathologicalClinical.NewRow();
                                    drPathologicalClinical[InfoPathologicalClinical.CLRISN] = this.clrNoiSoiISN;
                                    drPathologicalClinical[InfoPathologicalClinical.PLSISN] = list5[num8].ToString();
                                    dtPathologicalClinical.Rows.Add(drPathologicalClinical);

                                    objController.InsertRow(drPathologicalClinical, new string[] { });
                                }

                                ListDictionary paramUpdPathologicalOutList = new ListDictionary();
                                paramUpdPathologicalOutList.Add(InfoPathologicalOutList.CLRISN, this.clrNoiSoiISN);
                                paramUpdPathologicalOutList.Add(InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST, this.tbCDNgoaiDS.Text);
                                objController.Exec_StoreProc("sp_PathologicalOutList_Update", paramUpdPathologicalOutList);

                                ListDictionary paramUpdClinicalPathologicalSign = new ListDictionary();
                                paramUpdClinicalPathologicalSign.Add(InfoClinicalRecord.CLRISN, this.clrNoiSoiISN);
                                objController.Exec_StoreProc("sp_ClinicalPathologicalSign_Update", paramUpdClinicalPathologicalSign);

                                this.Insert = false;
                                this.NgayKham = this.dtpNgayKham.Text.ToString();
                                this.insertXetNghiem = false;
                                this.linkToaThuoc.Enabled = true;
                                this.existUpdated = true;
                            }

                            ///save thông tin tiền căn
                            ///edit by thaoht 20100107
                            ucTienCan.Save();

                            trans.CommitTransaction();
                            MessageBox.Show("Mẫu tin đã được cập nhật", "Cập nhật thành công", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        catch (Exception ex)
                        {
                            trans.RollbackTransaction();
                            Class.ShowException(ex);
                        }

                    }
                }
                catch (Exception exception)
                {
                    Class.ShowException(exception);
                }
            }
        }

        private void btCapNhatAnh_Click(object sender, EventArgs e)
        {
            this.NgayKham = this.dtpNgayKham.Text.ToString();
            frmNoiSoi soi = new frmNoiSoi(this.tbID.Text.ToString(), this.tbFullName.Text.ToString(), this.clrNoiSoiISN, this.Insert, this.NgayKham);
            soi.ShowInTaskbar = false;
            soi.ShowDialog();
        }

        private void btLapBenhAnMoi_Click(object sender, EventArgs e)
        {
            this.ThanhTien = "0";
            this.NhanBiet = string.Empty;
            this.ngayKham = DateTime.Now.ToString("dd/MM/yyyy");
            this.tbLanKham.Visible = true;
            this.cbETimes.Visible = false;
            int count = this.cbETimes.Items.Count;
            this.cbETimes.SelectedIndex = count - 1;
            this.tbLanKham.Text = Convert.ToString((int)(short.Parse(this.cbETimes.Text) + 1));
            this.cboTaiKham.Visible = false;
            this.txtTaiKham.Visible = true;
            this.txtTaiKham.Text = "0";
            this.tbNgayKham.Text = this.ngayKham;
            this.dtpNgayKham.Visible = false;
            this.tbNgayKham.Visible = true;
            this.btCapNhat.Enabled = false;
            this.btLuuBenhAn.Visible = true;
            this.linkNoiSoi.Enabled = false;
            this.linkToaThuoc.Enabled = false;
            this.linkXetNghiem.Enabled = false;
            this.linkCTPhauThuat.Enabled = false;//edit here 20090606
            this.listView1.Items.Clear();
            this.tbBenhNgoaiDS.Text = "";
            this.tbThuThuat.Text = "";
            this.tbCDNgoaiDS.Text = "";
            //this.tbPhauThuat.Text = "";
            gdPT.DataSource = null;
            this.tbLoiKhuyen.Text = "";
            this.lbCD.Items.Clear();
            this.cbBacSi.Visible = true;
            this.btLuuBenhAn.Enabled = true;
            this.btLapBenhAnMoi.Enabled = false;
            this.btTaiKham.Enabled = false;
            this.btCapNhat.Visible = false;
            //this.tbDienBien.Text = ""; edit here 20091017
            this.tbBSu.Text = "";
            this.tbMach.Text = "";
            this.tbNhietDo.Text = "";
            this.tbHuyetAp.Text = "";
            this.tbCanNang.Text = "";
            this.chkHetBenh.Checked = false;
            this.clrNoiSoiISN = string.Empty;
            this.dtpNgayTaiKham.Value = DateTime.Now;

            //load dien bien benh theo format lan kham/ lan tai kham 20091017
            LoadDienBienBenh(int.Parse(tbLanKham.Text), 0);

            /// load TIEN CAN moi
            /// edit by thaoht 20100107
            //loadThongTinTienCan("-9999");//= clrISN// 4 load benh an moi -- muc dich la xoa het du lieu trong cac control duoc sinh dong
            //---> 20100108 khong thuc hien vi no dang cho benh an, nhan nut lap benh an moi, se giu nguyen tien can cua benh an dang chon sang cho benh an
        }

        private void btLuuBenhAn_Click(object sender, EventArgs e)
        {
            this.ThanhTien = "0";
            this.ToaThuoc = "0";
            this.llablel = "daluu";
            if (this.checkData())
            {
                try
                {
                    if ((this.tbMach.Text.Length > 0) && !this.IsNumber(this.tbMach.Text.ToString()))
                    {
                        MessageBox.Show("Mạch không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbNhietDo.Text.Length > 0) && !this.IsFloat(this.tbNhietDo.Text.ToString()))
                    {
                        MessageBox.Show("Nhiệt độ không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else if ((this.tbCanNang.Text.Length > 0) && !this.IsFloat(this.tbCanNang.Text.ToString()))
                    {
                        MessageBox.Show("Cân nặng không hợp lệ", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        string str;
                        string str2;
                        string str3;
                        string str4;
                        string str5;
                        string str8;
                        if (this.chkHetBenh.Checked)
                        {
                            this.listView1.Items.Clear();
                            this.lbCD.Items.Clear();
                            this.tbBenhNgoaiDS.Text = "";
                            this.tbCDNgoaiDS.Text = "";
                        }
                        else
                        {
                            if ((this.listView1.Items.Count <= 0) && (this.tbBenhNgoaiDS.Text.Trim() == ""))
                            {
                                MessageBox.Show("Vui lòng chọn bệnh!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                return;
                            }
                            if ((this.lbCD.Items.Count <= 0) && (this.tbCDNgoaiDS.Text.Trim() == ""))
                            {
                                MessageBox.Show("Chẩn đoán không hợp lệ!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                return;
                            }
                        }
                        ///phau thuat
                        ///cong tat ca cac phau thuat ma user dang chon
                        string strTK4PT = string.Empty;
                        str = getStringPhauThuat(ref strTK4PT);

                        if (this.tbMach.Text == "")
                        {
                            str2 = "0";
                        }
                        else
                        {
                            str2 = this.tbMach.Text.ToString();
                        }
                        if (this.tbNhietDo.Text == "")
                        {
                            str3 = "0";
                        }
                        else
                        {
                            str3 = this.tbNhietDo.Text.ToString();
                        }
                        if (this.tbHuyetAp.Text == "")
                        {
                            str4 = "0";
                        }
                        else
                        {
                            str4 = this.tbHuyetAp.Text.ToString();
                        }
                        if (this.tbCanNang.Text == "")
                        {
                            str5 = "0";
                        }
                        else
                        {
                            str5 = this.tbCanNang.Text.ToString();
                        }

                        TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                        try
                        {
                            trans.BeginTransaction();

                            object intIdentity = objController.GetIDENT_CURRENT(InfoClinicalRecord.TABLENAME);
                            if (this.NhanBiet == "0")
                            {
                                this.clrISN = this.clrISN1;
                                this.clrNoiSoiISN = this.clrISN;
                            }
                            else
                            {
                                this.clrISN = Convert.ToString(Convert.ToInt64(intIdentity) + 1);
                                this.clrNoiSoiISN = this.clrISN;
                            }
                            this.ID = this.tbID.Text.ToString();

                            ArrayList list = new ArrayList();
                            ArrayList list2 = new ArrayList();
                            ArrayList list3 = new ArrayList();
                            int count = this.listView1.Items.Count;
                            for (int i = 0; i < count; i++)
                            {
                                string str6 = this.listView1.Items[i].Text.ToString();
                                switch (this.listView1.Items[i].ForeColor.Name.ToString())
                                {
                                    case "Red":
                                        list3.Add(0);
                                        break;

                                    case "Blue":
                                        list3.Add(1);
                                        break;

                                    case "Purple":
                                        list3.Add(2);
                                        break;

                                    case "Green":
                                        list3.Add(3);
                                        break;

                                    case "Gray":
                                        list3.Add(4);
                                        break;
                                }
                                list.Add(str6);
                            }

                            DataTable dtSick;
                            for (int j = 0; j < list.Count; j++)
                            {
                                List<ExpressionObject> paramSick = new List<ExpressionObject>();
                                paramSick.Add(new ExpressionObject(InfoSicks.SCKNAME, list[j].ToString()));
                                dtSick = objController.GetData(InfoSicks.TABLENAME, paramSick);

                                list2.Add(dtSick.Rows[0][0].ToString());
                            }

                            DataTable dtClinicalSick = InfoClinicalSick.GetStructure();
                            DataRow drClinicalSick;
                            for (int k = 0; k < list2.Count; k++)
                            {
                                drClinicalSick = dtClinicalSick.NewRow();
                                drClinicalSick[InfoClinicalSick.CLRISN] = this.clrISN;
                                drClinicalSick[InfoClinicalSick.SCKISN] = list2[k].ToString();
                                drClinicalSick[InfoClinicalSick.SCKSICKLEVEL] = list3[k].ToString();
                                dtClinicalSick.Rows.Add(drClinicalSick);
                                objController.InsertRow(drClinicalSick, new string[] { });
                            }

                            if (this.lbCD.Items.Count <= 0)
                            {
                                ListDictionary paramPathologicalOutList = new ListDictionary();
                                paramPathologicalOutList.Add(InfoPathologicalOutList.CLRISN, this.clrISN);
                                paramPathologicalOutList.Add(InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST, this.tbCDNgoaiDS.Text);
                                objController.Exec_StoreProc("sp_PathologicalOutList_Insert", paramPathologicalOutList);
                            }
                            else
                            {
                                ArrayList list4 = new ArrayList();
                                ArrayList list5 = new ArrayList();
                                int num5 = this.lbCD.Items.Count;
                                for (int m = 0; m < num5; m++)
                                {
                                    list4.Add(this.lbCD.Items[m].ToString());
                                }

                                DataTable dtPathologicalSigns;
                                for (int n = 0; n < list4.Count; n++)
                                {
                                    List<ExpressionObject> param = new List<ExpressionObject>();
                                    param.Add(new ExpressionObject(InfoPathologicalSigns.PLSPATHOLOGICALSIGNS, list4[n].ToString()));
                                    dtPathologicalSigns = objController.GetData(InfoPathologicalSigns.TABLENAME, param);

                                    list5.Add(dtPathologicalSigns.Rows[0][0].ToString());
                                }

                                DataTable dtPathologicalClinical = InfoPathologicalClinical.GetStructure();
                                DataRow drPathologicalClinical;
                                for (int num8 = 0; num8 < list5.Count; num8++)
                                {
                                    drPathologicalClinical = dtPathologicalClinical.NewRow();
                                    drPathologicalClinical[InfoPathologicalClinical.CLRISN] = this.clrISN;
                                    drPathologicalClinical[InfoPathologicalClinical.PLSISN] = list5[num8].ToString();
                                    dtPathologicalClinical.Rows.Add(drPathologicalClinical);
                                    objController.InsertRow(drPathologicalClinical, new string[] { });
                                }

                                ListDictionary paramPathologicalOutList = new ListDictionary();
                                paramPathologicalOutList.Add(InfoPathologicalOutList.CLRISN, this.clrISN);
                                paramPathologicalOutList.Add(InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST, this.tbCDNgoaiDS.Text);
                                objController.Exec_StoreProc("sp_PathologicalOutList_Insert", paramPathologicalOutList);
                            }
                            if (this.rbMale.Checked)
                            {
                                str8 = "Nam";
                            }
                            else
                            {
                                str8 = "Nữ";
                            }

                            DateTime time = DateTime.Now;
                            string str10 = string.Empty;
                            if (this.txtTaiKham.Text.Equals("0"))
                            {
                                str10 = "0";
                            }
                            else
                            {
                                str10 = "1";
                            }

                            ListDictionary paramClinicalRecord = new ListDictionary();
                            paramClinicalRecord.Add(InfoPatients.PATID, this.tbID.Text);
                            paramClinicalRecord.Add(InfoPatients.PATNAME, this.tbFullName.Text);
                            paramClinicalRecord.Add(InfoPatients.PATBIRTHDATE, this.dtpBirthDay.Value);
                            paramClinicalRecord.Add(InfoPatients.PATAGE, this.tbTuoi.Text);
                            paramClinicalRecord.Add(InfoPatients.PATSEX, str8);
                            paramClinicalRecord.Add(InfoPatients.PATADDRESS, this.tbAddress.Text);
                            paramClinicalRecord.Add(InfoPatients.PATTEL, this.tbPhone.Text);
                            paramClinicalRecord.Add(InfoPatients.PATOCCUPATION, this.tbJob.Text);
                            paramClinicalRecord.Add(InfoPatients.PATMOBILE, this.tbDiDong.Text);

                            //save Pic Of Patient 20091017
                            int intPatHasPic = savePicOfPatient();

                            paramClinicalRecord.Add(InfoPatients.PATHASPIC, intPatHasPic);// edit here 20091017
                            paramClinicalRecord.Add(InfoClinicalRecord.EMPISN, this.cbBacSi.SelectedValue);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRPULSE, str2);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRTEMPERATURE, str3);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRBLOODPRESSURE, str4);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRWEIGH, str5);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRMEDICALHISTORY, tbTienCan.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRTEST, string.Empty);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRSICKNOTINLIST, this.tbBenhNgoaiDS.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLREXAMINATIONTIMES, this.tbLanKham.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLREXAMINATIONDATE, DateTime.Now);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRSKILL, this.tbThuThuat.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRMEDICALADVICE, this.tbLoiKhuyen.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE, this.dtpNgayTaiKham.Value);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRPICTURE, string.Empty);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRSURGERY, str);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRAPPOINTMENTDATE, this.dtpNgayHen.Value);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRSICKCHANGE, this.tbDienBien.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRSICKHISTORY, this.tbBSu.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRREXAMINATIONTIMES, this.txtTaiKham.Text);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRREXAMINATIONSTATUS, str10);
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRTYPE, rbTMH.Checked == true ? "1" : "2"); //edit here 20090606; 1= TMH; 2= TM
                            paramClinicalRecord.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY, strTK4PT); //edit here 20090822
                            objController.Exec_StoreProc("sp_Patients_ClinicalRecord_Insert", paramClinicalRecord);

                            this.Ten = this.tbFullName.Text.ToString();
                            this.btLuuBenhAn.Visible = false;
                            this.btCapNhat.Visible = true; //edit here 20091017
                            this.btCapNhat.Enabled = true; //edit here 20091017
                            this.linkNoiSoi.LinkColor = Color.Blue;
                            this.linkXetNghiem.LinkColor = Color.Blue;
                            this.linkCTPhauThuat.LinkColor = Color.Blue; //edit here 20090606
                            this.linkNoiSoi.Enabled = true;
                            this.linkToaThuoc.Enabled = true;
                            this.linkXetNghiem.Enabled = true;
                            this.linkCTPhauThuat.Enabled = true; //edit here 20090606
                            this.insertXetNghiem = true;
                            this.NgayKham = this.dtpNgayKham.Text.ToString();
                            this.Insert = true;

                            ///save thông tin tiền căn
                            ///edit by thaoht 20100107
                            ucTienCan.resetExtraID1(this.clrISN);
                            ucTienCan.Save();

                            trans.CommitTransaction();
                            MessageBox.Show("Hồ sơ bệnh án đã được lưu", "Lưu hồ sơ bệnh án thành công", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        catch (Exception ex)
                        {
                            trans.RollbackTransaction();
                            Class.ShowException(ex);
                        }
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        private void btRemove_Click(object sender, EventArgs e)
        {
            this.listView1_DoubleClick(sender, e);
        }

        private void btRemoveChanDoan_Click(object sender, EventArgs e)
        {
            this.lbCD_DoubleClick(sender, e);
        }

        private void btTaiKham_Click(object sender, EventArgs e)
        {
            this.ThanhTien = "0";
            this.NhanBiet = string.Empty;
            this.ngayKham = DateTime.Now.ToString("dd/MM/yyyy");
            this.tbLanKham.Visible = true;
            this.cbETimes.Visible = false;
            this.tbLanKham.Text = this.cbETimes.Text;
            int num = this.cbETimes.FindString(this.tbLanKham.Text);
            this.cbETimes.SelectedIndex = num;
            this.cbETimes_SelectedIndexChanged(sender, e);
            this.cboTaiKham.Visible = false;
            this.txtTaiKham.Visible = true;

            //neu cung ngay kham thi lan tai kham khong tang len 20091017
            if (this.dtpNgayKham.Value.ToString("dd/MM/yyyy") != this.ngayKham)
            {
                if (this.cboTaiKham.Items.Count > 0)
                {
                    int count = this.cboTaiKham.Items.Count;
                    this.cboTaiKham.SelectedIndex = count - 1;
                    this.txtTaiKham.Text = Convert.ToString((int)(int.Parse(this.cboTaiKham.Text) + 1));
                }
                else
                {
                    this.txtTaiKham.Text = "1";
                }
                this.btCapNhat.Visible = false;
                this.btLuuBenhAn.Visible = true;
                this.btLuuBenhAn.Enabled = true;
                //load dien bien benh theo format lan kham/ lan tai kham 20091017
                LoadDienBienBenh(int.Parse(this.tbLanKham.Text), int.Parse(this.txtTaiKham.Text));

                ///load lai thong tin TIEN CAN
                ///edit by thaoht 20100107
                ///1. lay ra ma benh an truoc lan tai kham va lan kham moi nhat
                DataTable dtLatestCLRISN = objController.Getfn_MaxRexamTimesOFMaxExamTimesNoJoinMedExport(tbID.Text, this.tbLanKham.Text, this.txtTaiKham.Text);
                if (dtLatestCLRISN.Rows.Count == 0 || string.IsNullOrEmpty(dtLatestCLRISN.Rows[0][InfoClinicalRecord.OLDCLRISN].ToString()) )
                    loadThongTinTienCan("-9999");//= clrISN// 4 load benh an moi -- muc dich la xoa het du lieu trong cac control duoc sinh dong
                else
                    loadThongTinTienCan(dtLatestCLRISN.Rows[0][InfoClinicalRecord.OLDCLRISN].ToString());
            }
            else
            {
                this.txtTaiKham.Text = this.cboTaiKham.Text;
                this.btCapNhat.Visible = true; //vi luc nay khong phai la lap benh an moi ma chi la cap nhat benh an 
                this.btLuuBenhAn.Visible = false;
            }

            this.tbNgayKham.Text = this.ngayKham;
            this.dtpNgayKham.Visible = false;
            this.tbNgayKham.Visible = true;
            this.linkNoiSoi.Enabled = false;
            this.linkToaThuoc.Enabled = false;
            this.linkXetNghiem.Enabled = false;
            this.linkCTPhauThuat.Enabled = false; //edit here 20090606
            this.tbThuThuat.Text = "";
            //this.tbPhauThuat.Text = "";
            //gdPT.DataSource = null; -- edit here 20090830
            ///get thong tin phau thuat cua lan kham dang duoc chon hien tai
            try
            {
                string strLanKham = "0";
                if (cbETimes.Visible == false)
                {
                    strLanKham = tbLanKham.Text;
                }
                else
                {
                    strLanKham = cbETimes.SelectedItem.ToString();
                }
                DataTable dtSurgicalInfo = objController.getSurgicalInfo(this.tbID.Text, int.Parse(strLanKham));
                if (dtSurgicalInfo != null && dtSurgicalInfo.Rows.Count > 0)
                {
                    DataRow drSurgicalInfo = dtSurgicalInfo.Rows[0];
                    setDataSource4GridPhauThuat(drSurgicalInfo[InfoClinicalRecord.CLRSURGERY].ToString(), string.Empty);
                    if (!string.IsNullOrEmpty(drSurgicalInfo[InfoClinicalRecord.CLRSURGERY].ToString()))
                    {
                        this.dtpNgayHen.Value = DateTime.Parse(drSurgicalInfo[InfoClinicalRecord.CLRAPPOINTMENTDATE].ToString());
                    }
                    else
                    {
                        this.dtpNgayHen.Value = DateTime.Now;
                    }

                }
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
            this.tbLoiKhuyen.Text = "";
            this.cbBacSi.Visible = true;
            this.btLapBenhAnMoi.Enabled = false;
            this.btTaiKham.Enabled = false;
            //this.tbDienBien.Text = ""; --edit 20091017
            this.tbMach.Text = "";
            this.tbNhietDo.Text = "";
            this.tbHuyetAp.Text = "";
            this.tbCanNang.Text = "";
            this.chkHetBenh.Checked = false;
            this.clrNoiSoiISN = string.Empty;
            this.dtpNgayTaiKham.Value = DateTime.Now;
        }

        private void btThanhTien_Click(object sender, EventArgs e)
        {
            this.ID = this.tbID.Text;
            this.Ten = this.tbFullName.Text;
            this.Tuoi = this.tbTuoi.Text;
            if (this.rbMale.Checked)
            {
                this.Phai = "Nam";
            }
            else
            {
                this.Phai = "Nữ";
            }
            this.NgheNghiep = this.tbJob.Text;
            this.DiaChi = this.tbAddress.Text;
            this.ThuThuat = this.tbThuThuat.Text;
            this.NgayKham = this.dtpNgayKham.Text;
            this.NgayTaiKham = this.dtpNgayTaiKham.Text;
            this.BacSi = this.cbBacSi.Text;
            this.btLuuBenhAn.Enabled = false;
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            //xoa het cac file trong thu muc temp;
            //thaoht 20090723
            string strFilePath = System.Configuration.ConfigurationManager.AppSettings["LocalImagePath"].ToString();
            if (Directory.Exists(strFilePath) == true) //neu thu  muc ton tai
            {
                try
                {
                    Directory.Delete(strFilePath, true);
                }
                catch { }; //neu khong xoa duoc, vi li do file dang su dung, thi khong nhat thiet phai xoa ngay trong luc nay. de luc khac xoa cung duoc
            }

            base.Close();
        }

        private void btnInMaVach_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(InfoPatients.PATADDRESS, typeof(string));
            dt.Columns.Add(InfoPatients.PATBIRTHDATE, typeof(string));
            dt.Columns.Add(InfoPatients.PATSEX, typeof(string));
            dt.Columns.Add(InfoPatients.PATOCCUPATION, typeof(string));
            dt.Columns.Add(InfoPatients.PATTEL, typeof(string));
            dt.Columns.Add(InfoPatients.PATMOBILE, typeof(string));
            dt.Columns.Add(InfoPatients.PATNAME, typeof(string));
            dt.Columns.Add(InfoPatients.PATID, typeof(string));
            dt.Columns.Add(InfoClinicalRecord.CLRTYPE, typeof(string));
            DataRow dr = dt.NewRow();
            dr[InfoPatients.PATADDRESS] = this.tbAddress.Text;
            dr[InfoPatients.PATBIRTHDATE] = dtpBirthDay.Text;
            dr[InfoPatients.PATSEX] = rbFemale.Checked == true ? "Nữ" : "Nam";
            dr[InfoPatients.PATOCCUPATION] = this.tbJob.Text;
            dr[InfoPatients.PATTEL] = this.tbPhone.Text;
            dr[InfoPatients.PATMOBILE] = this.tbDiDong.Text;
            dr[InfoPatients.PATNAME] = this.tbFullName.Text;
            dr[InfoPatients.PATID] = this.tbID.Text;
            dr[InfoClinicalRecord.CLRTYPE] = rbTMH.Checked == true ? "TAI MŨI HỌNG" : "THẨM MỸ";
            dt.Rows.Add(dr);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            rptPatientCard patientcard = new rptPatientCard();
            patientcard.DataSource = dt;
            this.printDialog1.Document = this.printDocument1;
            DialogResult result = this.printDialog1.ShowDialog();
            PrintDocument document = new PrintDocument();
            if (result == DialogResult.OK)
            {
                patientcard.PrinterName = document.PrinterSettings.PrinterName;
                patientcard.PaperKind = PaperKind.A6;
                patientcard.Print();
            }
        }

        /// <summary>
        /// In phieu chup hinh phau thuat
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btPrintPT_Click(object sender, EventArgs e)
        {
            if (gdPT.DataSource == null)
                return;

            DataRow drFocus = gvPT.GetDataRow(gvPT.FocusedRowHandle);
            if (drFocus == null)
                return;

            if (cbETimes.SelectedItem == null || cboTaiKham.SelectedItem == null)
                return;
            ListDictionary param = new ListDictionary();
            param.Add(InfoSurgical.SURNAME, drFocus[InfoSurgical.SURNAME].ToString());
            object objSurSymbol = objController.GetScalar(InfoSurgical.SURSYMBOL, param, InfoSurgical.TABLENAME);
            if (objSurSymbol != null)
            {
                DataSet ds = new DataSet();
                DataTable dtPhieuChupHinh = new DataTable();
                dtPhieuChupHinh.Columns.Add(InfoPatients.PATID);
                dtPhieuChupHinh.Columns.Add(InfoSurgical.SURSYMBOL);
                dtPhieuChupHinh.Columns.Add("Suffix");

                DataRow dr = dtPhieuChupHinh.NewRow();
                dr[InfoPatients.PATID] = tbID.Text;
                dr["Suffix"] = objController.getSuffixFromExaminationDate(tbID.Text, int.Parse(cbETimes.SelectedItem.ToString()), int.Parse(cboTaiKham.SelectedItem.ToString()), dtpNgayKham.Value);
                dr[InfoSurgical.SURSYMBOL] = objSurSymbol.ToString();
                dtPhieuChupHinh.Rows.Add(dr);

                ds.Tables.Add(dtPhieuChupHinh);

                rptPhieuChup rpt = new rptPhieuChup();
                rpt.DataSource = ds;
                rpt.Print();
            }
            else
            {
                MessageBox.Show("Vui lòng nhập thông tin chữ viết tắt của phẫu thuật", "Thông Báo");
            }

        }
        #endregion

        private void frmHoSoBenhAn_Load(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();

            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoSickTree.SKTISN);
            arrColumn.Add(InfoSickTree.SKTNAME);
            //load cay benh
            ListDictionary orderby = new ListDictionary();
            orderby.Add(InfoSickTree.SKTNAME, "asc");
            DataTable dtSickTree_Sicks = objController.GetData("vw_SickTree_Sicks", true, arrColumn, null, orderby);

            if (dtSickTree_Sicks != null)
            {
                //load cay benh <trieu chung> da cap
                for (int i = 0; i < dtSickTree_Sicks.Rows.Count; i++)
                {
                    list.Add(dtSickTree_Sicks.Rows[i][InfoSickTree.SKTISN].ToString());//dtSickTree_Sicks.Rows[i][0].ToString()
                    list2.Add(dtSickTree_Sicks.Rows[i][InfoSickTree.SKTNAME].ToString());
                }

                ListDictionary orderbySicks = new ListDictionary();
                orderbySicks.Add(InfoSicks.SCKNAME, "asc");
                for (int j = 0; j < list.Count; j++)
                {
                    this.insertParentNode(list2[j].ToString());

                    List<ExpressionObject> param = new List<ExpressionObject>();
                    param.Add(new ExpressionObject(InfoSicks.SKTISN, list[j].ToString()));
                    DataTable dtSick = objController.GetData(InfoSicks.TABLENAME, param, orderbySicks);

                    for (int k = 0; k < dtSick.Rows.Count; k++)
                    {
                        this.insertChildNode(dtSick.Rows[k][2].ToString());
                    }
                    this.treeView1.Nodes.Add(this.node);
                }

                //load chan doan
                this.LoadChanDoan();

                //load bac si kham
                ArrayList arrCoEmployee = new ArrayList();
                arrCoEmployee.Add(InfoEmployees.EMPISN);
                arrCoEmployee.Add(InfoEmployees.EMPNAME);
                arrCoEmployee.Add(InfoEmployees.EMPSIGNATUREIMAGE); //edit by thaoht 20090604
                DataTable dtEmployee = objController.GetData(InfoEmployees.TABLENAME, arrCoEmployee, null);

                if (dtEmployee == null || dtEmployee.Rows.Count == 0)
                {
                    return;
                }
                this.cbBacSi.DisplayMember = InfoEmployees.EMPNAME;
                this.cbBacSi.ValueMember = InfoEmployees.EMPISN;
                this.cbBacSi.DataSource = dtEmployee;

                if (this.NhanBiet == "0") //chinh xac la truong hop khong bao gio xay ra. 20100108
                {
                    ///truong hop nay chi xay ra khi di tu danh sach ho so benh an 

                    //load thong tin benh nhan va benh an
                    ArrayList arrCoPatient = new ArrayList();
                    arrColumn.Add(InfoPatients.PATID);
                    arrColumn.Add(InfoPatients.PATNAME);
                    arrColumn.Add(InfoPatients.PATBIRTHDATE);
                    arrColumn.Add(InfoClinicalRecord.CLRPULSE);
                    arrColumn.Add(InfoClinicalRecord.CLRTEMPERATURE);
                    arrColumn.Add(InfoClinicalRecord.CLRBLOODPRESSURE);
                    arrColumn.Add(InfoClinicalRecord.CLRWEIGH);
                    arrColumn.Add(InfoPatients.PATSEX);
                    arrColumn.Add(InfoPatients.PATADDRESS);
                    arrColumn.Add(InfoPatients.PATAGE);
                    arrColumn.Add(InfoPatients.PATTEL);
                    arrColumn.Add(InfoPatients.PATOCCUPATION);
                    arrColumn.Add(InfoClinicalRecord.CLREXAMINATIONTIMES);
                    arrColumn.Add(InfoPatients.PATMOBILE);
                    arrColumn.Add(InfoClinicalRecord.CLRSURGERY);
                    arrColumn.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY); //edit here 20090822
                    arrColumn.Add(InfoPatients.PATHASPIC); /*20091017 */
                    
                    List<ExpressionObject> paramPatient = new List<ExpressionObject>();
                    paramPatient.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));

                    DataTable dtPatient = objController.GetData("vw_SearchClinicalRecord", false, arrCoPatient, paramPatient);

                    if (dtPatient != null && dtPatient.Rows.Count > 0)
                    {
                        DataRow drPatient = dtPatient.Rows[0];
                        this.tbID.Text = drPatient[InfoPatients.PATID].ToString();
                        this.tbFullName.Text = drPatient[InfoPatients.PATNAME].ToString();
                        this.dtpBirthDay.Value = DateTime.Parse(drPatient[InfoPatients.PATBIRTHDATE].ToString());

                        //set image 4 patient --edit 20091017
                        if (drPatient[InfoPatients.PATHASPIC].ToString() == "1")
                        {
                            loadPic4Patient();
                        }
                        else
                        {
                            this.picPatient.Image = null;
                        }

                        if (drPatient[InfoClinicalRecord.CLRPULSE].ToString() == "0")
                        {
                            this.tbMach.Text = "";
                        }
                        else
                        {
                            this.tbMach.Text = drPatient[InfoClinicalRecord.CLRPULSE].ToString();
                        }
                        if (drPatient[InfoClinicalRecord.CLRTEMPERATURE].ToString() == "0")
                        {
                            this.tbNhietDo.Text = "";
                        }
                        else
                        {
                            this.tbNhietDo.Text = drPatient[InfoClinicalRecord.CLRTEMPERATURE].ToString();
                        }
                        if (drPatient[InfoClinicalRecord.CLRBLOODPRESSURE].ToString() == "0")
                        {
                            this.tbHuyetAp.Text = "";
                        }
                        else
                        {
                            this.tbHuyetAp.Text = drPatient[InfoClinicalRecord.CLRBLOODPRESSURE].ToString();
                        }
                        if (drPatient[InfoClinicalRecord.CLRWEIGH].ToString() == "0")
                        {
                            this.tbCanNang.Text = "";
                        }
                        else
                        {
                            this.tbCanNang.Text = drPatient[InfoClinicalRecord.CLRWEIGH].ToString();
                        }
                        if (drPatient[InfoPatients.PATSEX].ToString() == "Nam")
                        {
                            this.rbMale.Checked = true;
                        }
                        else
                        {
                            this.rbFemale.Checked = true;
                        }
                        this.tbAddress.Text = drPatient[InfoPatients.PATADDRESS].ToString();
                        this.tbPhone.Text = drPatient[InfoPatients.PATTEL].ToString();
                        this.tbJob.Text = drPatient[InfoPatients.PATOCCUPATION].ToString();
                        this.tbDiDong.Text = drPatient[InfoPatients.PATMOBILE].ToString();
                        setDataSource4GridPhauThuat(drPatient[InfoClinicalRecord.CLRSURGERY].ToString(), drPatient[InfoClinicalRecord.CLRREXAMINATION4SURGERY].ToString());
                        this.tbNgayKham.Text = DateTime.Now.ToString("dd/MM/yyyy");
                        this.dtpNgayKham.Visible = false;
                        this.tbNgayKham.Visible = true;
                        this.cbBacSi.Visible = true;
                        this.tbLanKham.Text = "1";
                        this.tbLanKham.Visible = true;
                        this.cbETimes.Visible = false;
                        this.btLapBenhAnMoi.Visible = false;
                        this.btCapNhat.Enabled = false;
                        this.btLuuBenhAn.Enabled = true;
                        this.btLuuBenhAn.Visible = true;
                        this.btCapNhat.Visible = false;
                        this.linkNoiSoi.Enabled = false;
                        this.linkToaThuoc.Enabled = false;
                        this.linkXetNghiem.Enabled = false;
                        this.linkCTPhauThuat.Enabled = false; //edit here 20090606
                        dtpBirthDay_Leave(sender, e);//edit here 20090606
                        this.ThanhTien = "0";

                        ///thong thong tien can
                        ///edit by thaoht 20100108
                        loadThongTinTienCan("-9999");
                    }
                }
                else //frm lap ho so benh A, or frm tim kiem ho so benh an
                {
                    this.ThanhTien = "1";
                    this.dtpNgayKham.Visible = true;
                    this.tbNgayKham.Visible = false;
                    this.cbBacSi.Visible = true;
                    this.tbLanKham.Text = "1";
                    this.tbLanKham.Visible = false;
                    this.cbETimes.Visible = true;
                    this.btLapBenhAnMoi.Visible = true;
                    this.btCapNhat.Enabled = true;
                    this.linkNoiSoi.Enabled = true;
                    this.linkToaThuoc.Enabled = true;
                    this.linkXetNghiem.Enabled = true;
                    this.linkCTPhauThuat.Enabled = false; //edit here 20090606
                    this.btLuuBenhAn.Visible = false;

                    ArrayList arrCoClinicalRecord = new ArrayList();
                    arrCoClinicalRecord.Add(InfoClinicalRecord.CLREXAMINATIONTIMES);
                    List<ExpressionObject> paramClinicalRecord = new List<ExpressionObject>();
                    paramClinicalRecord.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));
                    DataTable dtClinicalRecord = objController.GetData(InfoClinicalRecord.TABLENAME, true, arrCoClinicalRecord, paramClinicalRecord);

                    if (dtClinicalRecord != null && dtClinicalRecord.Rows.Count > 0)
                    {
                        if ((dtClinicalRecord.Rows[0][InfoClinicalRecord.CLREXAMINATIONTIMES].ToString() == string.Empty) || (dtClinicalRecord.Rows[0][InfoClinicalRecord.CLREXAMINATIONTIMES].ToString() == ""))
                        {
                            this.tbLanKham.Text = "1";
                            this.tbLanKham.Visible = true;
                            this.cbETimes.Visible = false;
                            this.txtTaiKham.Text = "0";
                            this.txtTaiKham.Visible = true;
                            this.cboTaiKham.Visible = false;
                        }
                        else
                        {
                            this.cbETimes.Items.Clear();
                            for (int m = 0; m < dtClinicalRecord.Rows.Count; m++)
                            {
                                this.cbETimes.Items.Add(new ImageComboItem(dtClinicalRecord.Rows[m][InfoClinicalRecord.CLREXAMINATIONTIMES].ToString(), -1));
                            }
                            this.cbETimes.SelectedIndex = this.cbETimes.Items.Count - 1;
                        }
                    }
                    //20100108
                    //this.cbETimes_SelectedIndexChanged(null, null);  -- thaoht comment vi  this.cbETimes.SelectedIndex = this.cbETimes.Items.Count - 1; --> da chay ham cboETimes_selectedindexchanged roi
                }

                //load cac tien ich <hien tai phau thuat, thu thuat loi khuyen>
                //moi them 20090906 <tien can, dien tien benh, nghe nghiep, benh su>
                this.loadTienIch();
            }
        }

        #region Other Utility
        #region Picture 4 Patient
        private int savePicOfPatient()
        {
            if (_blnChangeImage == true && this.picPatient.Image != null)
            {
                string strFilePath = System.Configuration.ConfigurationManager.AppSettings["ServerImagePath4Patient"];
                if (Directory.Exists(strFilePath) == false) //neu thu  muc khong ton tai
                {
                    Directory.CreateDirectory(strFilePath);
                }

                strFilePath += System.IO.Path.DirectorySeparatorChar + this.tbID.Text + ".jpg";
                if (File.Exists(strFilePath) == true)
                {
                    File.Delete(strFilePath);
                }

                Image image = this.picPatient.Image;
                image.Save(strFilePath);
            }
            return (this.picPatient.Image == null) ? 0 : 1;
        }
        private void loadPic4Patient()
        {
            string strLocalPath = System.Configuration.ConfigurationManager.AppSettings["LocalImagePath4Patient"].ToString();
            string strServerPath = System.Configuration.ConfigurationManager.AppSettings["ServerImagePath4Patient"].ToString();
            strServerPath += System.IO.Path.DirectorySeparatorChar + this.tbID.Text + ".jpg";

            if (File.Exists(strServerPath))
            {
                if (Directory.Exists(strLocalPath) == false) //neu thu  muc khong ton tai
                {
                    Directory.CreateDirectory(strLocalPath);
                }
                strLocalPath += System.IO.Path.DirectorySeparatorChar + this.tbID.Text + ".jpg";

                if (File.Exists(strLocalPath) == false)
                {
                    File.Copy(strServerPath, strLocalPath);

                }
                Image img = new Bitmap(strLocalPath);
                this.picPatient.Image = img;
            }
        }
        #endregion

        #region PhauThuat
        /// <summary>
        /// get Chuoi phau Thuat
        /// </summary>
        /// <returns></returns>
        private string getStringPhauThuat(ref string strTK4PT)
        {
            if (gdPT.DataSource == null)
            {
                strTK4PT = string.Empty;
                return string.Empty;
            }
            DataTable dtPT = (DataTable)gdPT.DataSource;

            string strPT = string.Empty;
            foreach (DataRow dr in dtPT.Rows)
            {
                strPT += dr[InfoSurgical.SURNAME].ToString() + "\r\n- ";
                strTK4PT += (string.IsNullOrEmpty(dr[InfoClinicalRecord.CLRREXAMINATION4SURGERY].ToString()) == true ? "0" : dr[InfoClinicalRecord.CLRREXAMINATION4SURGERY].ToString()) + ";";
            }
            return strPT;
        }

        /// <summary>
        /// set DataSource cho grid PhauThuat, tu chuoi PT co truoc
        /// </summary>
        /// <param name="strPhauThuat"></param>
        private void setDataSource4GridPhauThuat(string strPhauThuat, string strTaiKham)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(InfoSurgical.SURNAME, typeof(string));
                dt.Columns.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY, typeof(int));
                DataRow dr;
                strPhauThuat = strPhauThuat.StartsWith("-") == true ? strPhauThuat.Remove(0, 1) : strPhauThuat;
                strPhauThuat = strPhauThuat.Replace("\r\n- ", ";");
                string[] taikham = strTaiKham.Split(';');
                int count = 0;
                foreach (string strTemp in strPhauThuat.Split(';'))
                {
                    if (string.IsNullOrEmpty(strTemp))
                    {
                        count++;
                        continue;
                    }
                    dr = dt.NewRow();
                    dr[InfoSurgical.SURNAME] = strTemp.Trim();
                    dr[InfoClinicalRecord.CLRREXAMINATION4SURGERY] = string.IsNullOrEmpty(strTaiKham) == true ? 0 : string.IsNullOrEmpty(taikham.GetValue(count).ToString()) == true ? 0 : int.Parse(taikham.GetValue(count).ToString());
                    dt.Rows.Add(dr);
                    count++;
                }
                gdPT.DataSource = dt;
            }
            catch (Exception ex)
            {
                Class.ShowException(ex);
            }
        }
        #endregion

        private void LoadDienBienBenh(int intLanKham, int intLanTaiKham)
        {
            /* -	Phần diễn biến bệnh thì chèn vào lần khám và tái khám. Sau mỗi lần tái khám thì máy tự động chèn vào: BS khám chỉ chèn nội dung vào thôi
             -	VD:
             1.	Lần khám 1:
             1.1.	Lần tái khám 0 (ngày/tháng/năm): 
             1.2.	Lần tái khám 1 (ngày/tháng/năm):
             1.3.	Lần tái khám 2 (ngày/tháng/năm):
             2.	Lần khám 2:
             2.1.	 Lần tái khám 0 (ngày/tháng/năm): 
             2.2.	Lần tái khám 1 (ngày/tháng/năm):
             2.3.	Lần tái khám 2 (ngày/tháng/năm): */
            if (intLanKham == 1 && intLanTaiKham == 0)
            {
            }
            else
            {
                this.tbDienBien.Text += "\r\n";
            }
            if (intLanTaiKham == 0)
            {
                this.tbDienBien.Text += intLanKham.ToString() + ". Lần khám " + intLanKham.ToString() + ": \r\n";
            }
            this.tbDienBien.Text += intLanKham.ToString() + "." + (intLanTaiKham + 1).ToString() + ". \t Lần tái khám " + intLanTaiKham.ToString() + " (" + DateTime.Now.ToString("dd/MM/yyyy") + "): \r\n";
        }

        private void changeLanKham(List<ExpressionObject> param)
        {
            this.Insert = false;
            this.ThanhTien = "1";
            this.ToaThuoc = "1";

            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoPatients.PATID);
            arrColumn.Add(InfoPatients.PATNAME);
            arrColumn.Add(InfoPatients.PATAGE);
            arrColumn.Add(InfoPatients.PATSEX);
            arrColumn.Add(InfoPatients.PATADDRESS);
            arrColumn.Add(InfoPatients.PATTEL);
            arrColumn.Add(InfoPatients.PATOCCUPATION);
            arrColumn.Add(InfoPatients.PATHASPIC); //edit here 20091017
            arrColumn.Add(InfoClinicalRecord.CLRPULSE);
            arrColumn.Add(InfoClinicalRecord.CLRTEMPERATURE);
            arrColumn.Add(InfoClinicalRecord.CLRBLOODPRESSURE);
            arrColumn.Add(InfoClinicalRecord.CLRWEIGH);
            arrColumn.Add(InfoClinicalRecord.CLRMEDICALHISTORY);
            arrColumn.Add(InfoClinicalRecord.CLRTEST);
            arrColumn.Add(InfoClinicalRecord.CLRSICKNOTINLIST);
            arrColumn.Add(InfoClinicalRecord.CLRPATHOLOGICALSIGNS);
            arrColumn.Add(InfoClinicalRecord.CLREXAMINATIONTIMES);
            arrColumn.Add(InfoClinicalRecord.CLRSKILL);
            arrColumn.Add(InfoClinicalRecord.CLRDOCTORPRESCRIPTION);
            arrColumn.Add(InfoClinicalRecord.CLRMEDICALADVICE);
            arrColumn.Add(InfoPatients.PATBIRTHDATE);
            arrColumn.Add(InfoClinicalRecord.CLREXAMINATIONDATE);
            arrColumn.Add(InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE);
            arrColumn.Add(InfoEmployees.EMPNAME);
            arrColumn.Add(InfoClinicalRecord.CLRPICTURESTATUS);
            arrColumn.Add(InfoClinicalRecord.CLRISN);
            arrColumn.Add(InfoPatients.PATMOBILE);
            arrColumn.Add(InfoClinicalRecord.CLRSURGERY);
            arrColumn.Add(InfoClinicalRecord.CLRAPPOINTMENTDATE);
            arrColumn.Add(InfoClinicalRecord.CLRSICKCHANGE);
            arrColumn.Add(InfoClinicalRecord.CLRSICKHISTORY);
            arrColumn.Add(InfoClinicalRecord.CLRTYPE); //edit here 20090606
            arrColumn.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY); // edit here 20090822

            DataTable dtClinicalRecord = objController.GetData("vw_ClinicalRecord", param);

            if (dtClinicalRecord != null && dtClinicalRecord.Rows.Count > 0)
            {
                DataRow drClinicalRecord = dtClinicalRecord.Rows[0];
                this.tbNgayKham.Visible = false;
                this.dtpNgayKham.Visible = true;
                this.clrNoiSoiISN = drClinicalRecord[InfoClinicalRecord.CLRISN].ToString();
                this.tbID.Text = drClinicalRecord[InfoPatients.PATID].ToString();
                this.tbFullName.Text = drClinicalRecord[InfoPatients.PATNAME].ToString();

                ///load lai thong tin TIEN CAN theo benh an duoc chon
                ///edit by thaoht 20100107
                loadThongTinTienCan(this.clrNoiSoiISN);

                //set image 4 patient --edit 20091017
                if (drClinicalRecord[InfoPatients.PATHASPIC].ToString() == "1")
                {
                    loadPic4Patient();
                }
                else
                {
                    this.picPatient.Image = null;
                }

                if (drClinicalRecord[InfoPatients.PATSEX].ToString() == "Nam")
                {
                    this.rbMale.Checked = true;
                }
                else
                {
                    this.rbFemale.Checked = true;
                }
                this.tbAddress.Text = drClinicalRecord[InfoPatients.PATADDRESS].ToString();
                this.tbPhone.Text = drClinicalRecord[InfoPatients.PATTEL].ToString();
                this.tbJob.Text = drClinicalRecord[InfoPatients.PATOCCUPATION].ToString();
                if (drClinicalRecord[InfoClinicalRecord.CLRPULSE].ToString() == "0")
                {
                    this.tbMach.Text = "";
                }
                else
                {
                    this.tbMach.Text = drClinicalRecord[InfoClinicalRecord.CLRPULSE].ToString();
                }
                if (drClinicalRecord[InfoClinicalRecord.CLRTEMPERATURE].ToString() == "0")
                {
                    this.tbNhietDo.Text = "";
                }
                else
                {
                    this.tbNhietDo.Text = drClinicalRecord[InfoClinicalRecord.CLRTEMPERATURE].ToString();
                }
                if (drClinicalRecord[InfoClinicalRecord.CLRBLOODPRESSURE].ToString() == "0")
                {
                    this.tbHuyetAp.Text = "";
                }
                else
                {
                    this.tbHuyetAp.Text = drClinicalRecord[InfoClinicalRecord.CLRBLOODPRESSURE].ToString();
                }
                if (drClinicalRecord[InfoClinicalRecord.CLRWEIGH].ToString() == "0")
                {
                    this.tbCanNang.Text = "";
                }
                else
                {
                    this.tbCanNang.Text = drClinicalRecord[InfoClinicalRecord.CLRWEIGH].ToString();
                }
                this.tbTienCan.Text = drClinicalRecord[InfoClinicalRecord.CLRMEDICALHISTORY].ToString();
                this.tbBenhNgoaiDS.Text = drClinicalRecord[InfoClinicalRecord.CLRSICKNOTINLIST].ToString();

                //edit here 20090606
                rbTMH.Checked = string.IsNullOrEmpty(drClinicalRecord[InfoClinicalRecord.CLRTYPE].ToString()) == true || drClinicalRecord[InfoClinicalRecord.CLRTYPE].ToString() == "1" ? true : false; //1 : TMH ; 2: TM
                rbTM.Checked = !rbTMH.Checked;

                this.tbThuThuat.Text = drClinicalRecord[InfoClinicalRecord.CLRSKILL].ToString();
                setDataSource4GridPhauThuat(drClinicalRecord[InfoClinicalRecord.CLRSURGERY].ToString(), drClinicalRecord[InfoClinicalRecord.CLRREXAMINATION4SURGERY].ToString());

                //neu có thủ thuật thì phẫu thuật sẽ không có. và người lại --edit 20090606
                if (string.IsNullOrEmpty(drClinicalRecord[InfoClinicalRecord.CLRSKILL].ToString().Trim()) == false)
                {
                    grpThuThuat.Selected = true;
                    grpPhauThuat.Selected = false;
                }
                else
                {
                    grpThuThuat.Selected = false;
                    grpPhauThuat.Selected = true;
                }

                this.tbLoiKhuyen.Text = drClinicalRecord[InfoClinicalRecord.CLRMEDICALADVICE].ToString();
                if (drClinicalRecord[InfoEmployees.EMPNAME].ToString().Trim() == "")
                {
                    this.cbBacSi.SelectedIndex = -1;
                    this.linkToaThuoc.Enabled = false;
                }
                else
                {
                    this.cbBacSi.SelectedIndex = this.cbBacSi.FindString(drClinicalRecord[InfoEmployees.EMPNAME].ToString());
                    this.linkToaThuoc.Enabled = true;
                }

                this.dtpBirthDay.Value = DateTime.Parse(drClinicalRecord[InfoPatients.PATBIRTHDATE].ToString());
                this.dtpBirthDay_Leave(null, null); //edit here 20090606
                this.dtpNgayKham.Value = DateTime.Parse(drClinicalRecord[InfoClinicalRecord.CLREXAMINATIONDATE].ToString());
                this.dtpNgayTaiKham.Value = DateTime.Parse(drClinicalRecord[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE].ToString());
                this.ImagePath = drClinicalRecord[InfoClinicalRecord.CLRPICTURE].ToString();
                this.clrImageISN = drClinicalRecord[InfoClinicalRecord.CLRISN].ToString();
                this.tbDiDong.Text = drClinicalRecord[InfoPatients.PATMOBILE].ToString();

                this.tbBSu.Text = drClinicalRecord[InfoClinicalRecord.CLRSICKHISTORY].ToString();
                if (!string.IsNullOrEmpty(drClinicalRecord[InfoClinicalRecord.CLRSURGERY].ToString()))
                {
                    this.dtpNgayHen.Text = drClinicalRecord[InfoClinicalRecord.CLRAPPOINTMENTDATE].ToString();
                }
                this.tbDienBien.Text = drClinicalRecord[InfoClinicalRecord.CLRSICKCHANGE].ToString();

                DataTable dtClinicalSicks = objController.GetData("vw_ClinicalSicks", param);

                if (dtClinicalSicks != null)
                {
                    this.listView1.Items.Clear();
                    for (int i = 0; i < dtClinicalSicks.Rows.Count; i++)
                    {
                        ListViewItem item = new ListViewItem(dtClinicalSicks.Rows[i][InfoSicks.SCKNAME].ToString());
                        switch (dtClinicalSicks.Rows[i][InfoClinicalSick.SCKSICKLEVEL].ToString())
                        {
                            case "0":
                                item.ForeColor = Color.Red;
                                break;

                            case "1":
                                item.ForeColor = Color.Blue;
                                break;

                            case "2":
                                item.ForeColor = Color.Purple;
                                break;

                            case "3":
                                item.ForeColor = Color.Green;
                                break;

                            case "4":
                                item.ForeColor = Color.Gray;
                                break;
                        }
                        this.listView1.Items.AddRange(new ListViewItem[] { item });
                    }

                    DataTable dtPathologicalClinical = objController.GetData("vw_PathologicalClinical", param);

                    if (dtPathologicalClinical == null || dtPathologicalClinical.Rows.Count == 0)
                        this.linkToaThuoc.Enabled = false;
                    else if (dtPathologicalClinical.Rows[0][InfoPathologicalSigns.PLSPATHOLOGICALSIGNS].ToString() == "")
                    {
                        this.linkToaThuoc.Enabled = false;
                        this.lbCD.Items.Clear(); //vì không có chẩn đoán trong danh sách cây chẩn đoán
                        this.tbCDNgoaiDS.Text = dtPathologicalClinical.Rows[0][InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST].ToString();
                    }
                    else
                    {
                        this.lbCD.Items.Clear();
                        for (int j = 0; j < dtPathologicalClinical.Rows.Count; j++)
                        {
                            this.lbCD.Items.Add(dtPathologicalClinical.Rows[j][InfoPathologicalSigns.PLSPATHOLOGICALSIGNS].ToString());
                        }
                        this.tbCDNgoaiDS.Text = dtPathologicalClinical.Rows[0][InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST].ToString();
                    }

                    this.btTaiKham.Enabled = true;

                    this.insertXetNghiem = false;
                    this.linkXetNghiem.Enabled = true;
                    this.linkCTPhauThuat.Enabled = true; //edit here 20090606
                    this.dtpNgayHen.CustomFormat = "dd/MM/yyyy";
                    this.NgayKham = this.dtpNgayKham.Text.ToString();
                    if (!this.KTKhoiBenh())
                    {
                        this.chkHetBenh.Checked = true;
                        this.btCapNhat.Enabled = false;
                    }
                    else
                    {
                        this.chkHetBenh.Checked = false;
                        this.btCapNhat.Enabled = true;
                    }

                    ListDictionary paramCLRISN = new ListDictionary();
                    paramCLRISN.Add(InfoAnalyzeResult.CLRISN, clrNoiSoiISN);
                    object objNLPISN = objController.Count(InfoAnalyzeResult.NLPISN, paramCLRISN, "vw_AnalyzeResult");
                    if (objNLPISN == null || (int)objNLPISN == 0)
                    {
                        this.linkXetNghiem.LinkColor = Color.Blue;
                    }
                    else
                    {
                        this.linkXetNghiem.LinkColor = Color.DarkGoldenrod;
                    }

                    object iCLRISN = objController.Count(InfoScanResult.CLRISN, paramCLRISN, "vw_ScanResult_InnerScan");

                    if (iCLRISN == null || (int)iCLRISN == 0)
                    {
                        this.linkNoiSoi.LinkColor = Color.Blue;
                    }
                    else
                    {
                        this.linkNoiSoi.LinkColor = Color.DarkGoldenrod;
                    }

                    ///cần kiểm tra xem có CTPhauThuat khong???
                    ///nếu co thì tô màu khác---
                    iCLRISN = objController.Count(InfoClinicalRecord.CLRISN, paramCLRISN, InfoSurgeryDetail.TABLENAME);

                    if (iCLRISN == null || (iCLRISN is int && (int)iCLRISN == 0))
                    {
                        _blnInsertSurgeryDetail = true;
                        this.linkCTPhauThuat.LinkColor = Color.Blue;
                    }
                    else
                    {
                        _blnInsertSurgeryDetail = false;
                        this.linkCTPhauThuat.LinkColor = Color.DarkGoldenrod;
                    }
                }
            }
        }

        private void insertChildNode(string strName)
        {
            this.node.Nodes.Add(strName);
        }

        private void insertParentNode(string strName)
        {
            this.node = new TreeNode(strName);
        }

        private bool checkData()
        {
            for (int i = 0; i < this.lbCD.Items.Count; i++)
            {
                for (int j = 0; j < this.lbCD.Items.Count; j++)
                {
                    if (this.lbCD.Items[j].ToString() == "")
                    {
                        this.lbCD.Items.Remove(this.lbCD.Items[j]);
                    }
                }
            }
            if (this.tbFullName.Text == "")
            {
                MessageBox.Show("Vui lòng nhập tên bệnh nhân!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (((this.tbTuoi.Text == "") || (int.Parse(this.tbTuoi.Text.ToString()) < 0)) || (int.Parse(this.tbTuoi.Text.ToString()) > 120))
            {
                MessageBox.Show("Vui lòng nhập ngày tháng năm sinh hợp lệ!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.tbAddress.Text == "")
            {
                MessageBox.Show("Vui lòng nhập địa chỉ!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.cbBacSi.Text == "")
            {
                MessageBox.Show("Vui lòng chọn bác sĩ khám!", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }

        private bool IsFloat(string Number)
        {
            try
            {
                float.Parse(Number);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool IsNumber(string Number)
        {
            try
            {
                decimal.Parse(Number);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool KTKhoiBenh()
        {
            bool flag;
            bool flag2;
            bool flag3;
            bool flag4;
            object objClrISN;
            ListDictionary param = new ListDictionary();
            param.Add(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text);
            if (this.cboTaiKham.Visible)
            {
                param.Add(InfoClinicalRecord.CLRREXAMINATIONTIMES, this.cboTaiKham.Text);
            }
            objClrISN = objController.GetScalar(InfoClinicalRecord.CLRISN, param, InfoClinicalRecord.TABLENAME);
            if (objClrISN is int)
            {
                string str2 = objClrISN.ToString();
                ListDictionary param2 = new ListDictionary();
                param2.Add(InfoClinicalRecord.CLRISN, objClrISN);
                object objPathologicalClinical = objController.Count("0", param2, InfoPathologicalClinical.TABLENAME);

                if (objPathologicalClinical == null || (int)objPathologicalClinical == 0)
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                }

                object objPathologicalOutList = objController.GetScalar(InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST, param2, InfoPathologicalOutList.TABLENAME);
                if (objPathologicalOutList == null || string.IsNullOrEmpty(objPathologicalOutList.ToString()))
                {
                    flag2 = false;
                }
                else
                {
                    flag2 = true;
                }

                object objSCKISN = objController.Count(InfoClinicalSick.SCKISN, param2, InfoClinicalSick.TABLENAME);
                if (objSCKISN == null || (int)objSCKISN == 0)
                {
                    flag3 = false;
                }
                else
                {
                    flag3 = true;
                }

                object objCLRSICKNOTINLIST = objController.GetScalar(InfoClinicalRecord.CLRSICKNOTINLIST, param2, InfoClinicalRecord.TABLENAME);
                if (objCLRSICKNOTINLIST == null || string.IsNullOrEmpty(objCLRSICKNOTINLIST.ToString()))
                {
                    flag4 = false;
                }
                else
                {
                    flag4 = true;
                }
                if ((!flag && !flag2) && (!flag3 && !flag4))
                {
                    return false;
                }
            }

            return true;
        }

        private void LoadChanDoan()
        {
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();

            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoPathologicalTree.PLTISN);
            arrColumn.Add(InfoPathologicalTree.PLTNAME);
            ListDictionary orderby = new ListDictionary();
            orderby.Add(InfoPathologicalTree.PLTNAME, "asc");
            DataTable dtPathologicalSign = objController.GetData("vw_PathologicalSign", true, arrColumn, null, orderby);

            if (dtPathologicalSign != null)
            {
                for (int i = 0; i < dtPathologicalSign.Rows.Count; i++)
                {
                    list.Add(dtPathologicalSign.Rows[i][0].ToString());
                    list2.Add(dtPathologicalSign.Rows[i][1].ToString());
                }

                ListDictionary orderby2 = new ListDictionary();
                orderby.Add(InfoPathologicalSigns.PLSPATHOLOGICALSIGNS, "asc");

                List<ExpressionObject> param = new List<ExpressionObject>();

                for (int j = 0; j < list.Count; j++)
                {
                    this.insertParentNode(list2[j].ToString());
                    param.Clear();
                    param.Add(new ExpressionObject(InfoPathologicalSigns.PLTISN, list[j].ToString()));
                    DataTable dtPathologicalSign2 = objController.GetData(InfoPathologicalSigns.TABLENAME, false, null, param, orderby2);

                    for (int k = 0; k < dtPathologicalSign2.Rows.Count; k++)
                    {
                        this.insertChildNode(dtPathologicalSign2.Rows[k][2].ToString());
                    }
                    this.treeView2.Nodes.Add(this.node);
                }
            }
        }

        private void loadTienIch()
        {
            ListDictionary orderby4Advice = new ListDictionary();
            orderby4Advice.Add(InfoAdvice.ADVDOCTORADVICE, "asc");
            DataTable dtAdvice = objController.GetData(InfoAdvice.TABLENAME, null, orderby4Advice);
            for (int i = 0; i < dtAdvice.Rows.Count; i++)
            {
                MenuItem item = new MenuItem();
                item.Text = dtAdvice.Rows[i][1].ToString();
                this.ctmLoiKhuyen.MenuItems.Add(i, item);
                this.ctmLoiKhuyen.MenuItems[i].Click += new EventHandler(this.AddLoiKhuyen);
            }

            ListDictionary orderby4TreatmentSkill = new ListDictionary();
            orderby4TreatmentSkill.Add(InfoTreatmentSkill.TRSSKILLNAME, "asc");
            DataTable dtTreatmentSkill = objController.GetData(InfoTreatmentSkill.TABLENAME, null, orderby4TreatmentSkill);
            for (int j = 0; j < dtTreatmentSkill.Rows.Count; j++)
            {
                MenuItem item2 = new MenuItem();
                item2.Text = dtTreatmentSkill.Rows[j][1].ToString();
                this.ctmThuThuat.MenuItems.Add(j, item2);
                this.ctmThuThuat.MenuItems[j].Click += new EventHandler(this.AddThuThuat);
            }

            DataTable dtSurgical = objController.GetData(InfoSurgical.TABLENAME);
            DataRow[] drFoundArr = dtSurgical.Select("", InfoSurgical.SURNAME);
            foreach (DataRow drSurgical in drFoundArr)
            {
                this.ctmPhauThuat.Items.Add(drSurgical[InfoSurgical.SURNAME].ToString());
                this.ctmPhauThuat.Items[this.ctmPhauThuat.Items.Count - 1].Click += new EventHandler(this.AddPhauThuat);
            }

            //load them tien can, dien bien benh, nghe nghiep, benh su
            DataTable dtFacility = objController.GetData(InfoFacility.TABLENAME);
            foreach (DataRow drFacility in dtFacility.Rows)
            {
                switch (int.Parse(drFacility[InfoFacility.FACTYPE].ToString()))
                {
                    case (int)InfoFacility.eFacilityType._eBenhSu:
                        this.ctmBenhSu.Items.Add(drFacility[InfoFacility.FACNAME].ToString());
                        this.ctmBenhSu.Items[this.ctmBenhSu.Items.Count - 1].Click += new EventHandler(this.AddBenhSu);
                        break;
                    case (int)InfoFacility.eFacilityType._eDienTienBenh:
                        this.ctmDienBienBenh.Items.Add(drFacility[InfoFacility.FACNAME].ToString());
                        this.ctmDienBienBenh.Items[this.ctmDienBienBenh.Items.Count - 1].Click += new EventHandler(this.AddDienBienBenh);
                        break;
                    case (int)InfoFacility.eFacilityType._eNgheNghiep:
                        this.ctmNgheNghiep.Items.Add(drFacility[InfoFacility.FACNAME].ToString());
                        this.ctmNgheNghiep.Items[this.ctmNgheNghiep.Items.Count - 1].Click += new EventHandler(this.AddNgheNghiep);
                        break;
                    case (int)InfoFacility.eFacilityType._eTienCan:
                        this.ctmTienCan.Items.Add(drFacility[InfoFacility.FACNAME].ToString());
                        this.ctmTienCan.Items[this.ctmTienCan.Items.Count - 1].Click += new EventHandler(this.AddTienCan);
                        break;
                }
            }
        }

        #endregion

        #region Event
        private void cbETimes_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoClinicalRecord.CLRREXAMINATIONSTATUS);
            arrColumn.Add(InfoClinicalRecord.CLRREXAMINATIONTIMES);
            arrColumn.Add(InfoClinicalRecord.CLRPICTURESTATUS);

            List<ExpressionObject> param = new List<ExpressionObject>();
            param.Add(new ExpressionObject(InfoClinicalRecord.PATISN, this.patISN));
            param.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
            DataTable dtClinicalRecord = objController.GetData(InfoClinicalRecord.TABLENAME, true, arrColumn, param);

            if (dtClinicalRecord == null || dtClinicalRecord.Rows.Count == 0 || dtClinicalRecord.Rows[0][InfoClinicalRecord.CLRREXAMINATIONSTATUS].ToString() == string.Empty)
            {
                this.txtTaiKham.Text = "0";
                this.txtTaiKham.Visible = true;
                this.cboTaiKham.Visible = false;
            }
            else
            {
                this.btTaiKham.Enabled = true;
                this.txtTaiKham.Visible = false;
                this.cboTaiKham.Visible = true;
                this.cboTaiKham.Items.Clear();
                this.cboTaiKham.ImageList = this.imageList1;
                for (int i = 0; i < dtClinicalRecord.Rows.Count; i++)
                {
                    if (dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRPICTURESTATUS].ToString() == "0")
                    {
                        this.cboTaiKham.Items.Add(dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRREXAMINATIONTIMES].ToString());
                    }
                    else if (dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRPICTURESTATUS].ToString() == "1")
                    {
                        this.cboTaiKham.Items.Add(new ImageComboItem(dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRREXAMINATIONTIMES].ToString(), 0));
                    }
                    else if (dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRPICTURESTATUS].ToString() == "2")
                    {
                        this.cboTaiKham.Items.Add(new ImageComboItem(dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRREXAMINATIONTIMES].ToString(), 1));
                    }
                    else
                    {
                        this.cboTaiKham.Items.Add(new ImageComboItem(dtClinicalRecord.Rows[i][InfoClinicalRecord.CLRREXAMINATIONTIMES].ToString(), 2));
                    }
                }
                this.cboTaiKham.SelectedIndex = this.cboTaiKham.Items.Count - 1;
                return;
            }
            this.Insert = false;
            this.ThanhTien = "1";
            this.ToaThuoc = "1";

            ArrayList arrCoPatients = new ArrayList();
            arrCoPatients.Add(InfoPatients.PATID);
            arrCoPatients.Add(InfoPatients.PATNAME);
            arrCoPatients.Add(InfoPatients.PATAGE);
            arrCoPatients.Add(InfoPatients.PATSEX);
            arrCoPatients.Add(InfoPatients.PATADDRESS);
            arrCoPatients.Add(InfoPatients.PATTEL);
            arrCoPatients.Add(InfoPatients.PATOCCUPATION);
            arrCoPatients.Add(InfoPatients.PATHASPIC); /* edit 20091017 */
            arrCoPatients.Add(InfoClinicalRecord.CLRPULSE);
            arrCoPatients.Add(InfoClinicalRecord.CLRTEMPERATURE);
            arrCoPatients.Add(InfoClinicalRecord.CLRBLOODPRESSURE);
            arrCoPatients.Add(InfoClinicalRecord.CLRWEIGH);
            arrCoPatients.Add(InfoClinicalRecord.CLRMEDICALHISTORY);
            arrCoPatients.Add(InfoClinicalRecord.CLRTEST);
            arrCoPatients.Add(InfoClinicalRecord.CLRSICKNOTINLIST);
            arrCoPatients.Add(InfoClinicalRecord.CLRPATHOLOGICALSIGNS);
            arrCoPatients.Add(InfoClinicalRecord.CLREXAMINATIONTIMES);
            arrCoPatients.Add(InfoClinicalRecord.CLRSKILL);
            arrCoPatients.Add(InfoClinicalRecord.CLRDOCTORPRESCRIPTION);
            arrCoPatients.Add(InfoClinicalRecord.CLRMEDICALADVICE);
            arrCoPatients.Add(InfoPatients.PATBIRTHDATE);//replace for "DAY(patBirthDate) As NgaySinh, MONTH(patBirthDate) As ThangSinh, YEAR(patBirthDate) As NamSinh"
            arrCoPatients.Add(InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE);// replace for "DAY(clrFollowUpExaminationDate) As NgayTaiKham, MONTH(clrFollowUpExaminationDate) As ThangTaiKham, YEAR(clrFollowUpExaminationDate) As NamTaiKham"
            arrCoPatients.Add(InfoEmployees.EMPNAME);
            arrCoPatients.Add(InfoClinicalRecord.CLRPICTURE);
            arrCoPatients.Add(InfoClinicalRecord.CLRISN);
            arrCoPatients.Add(InfoPatients.PATMOBILE);
            arrCoPatients.Add(InfoClinicalRecord.CLRSURGERY);
            arrCoPatients.Add(InfoClinicalRecord.CLRAPPOINTMENTDATE);
            arrCoPatients.Add(InfoClinicalRecord.CLRSICKCHANGE);
            arrCoPatients.Add(InfoClinicalRecord.CLRSICKHISTORY);
            arrCoPatients.Add(InfoClinicalRecord.CLRTYPE); //edit here 20090606
            arrCoPatients.Add(InfoClinicalRecord.CLRREXAMINATION4SURGERY); //edit here 20090822

            List<ExpressionObject> paramFullClinicalRecord = new List<ExpressionObject>();
            paramFullClinicalRecord.Add(new ExpressionObject(InfoClinicalRecord.PATISN, this.patISN));
            paramFullClinicalRecord.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
            DataTable dtFullClinicalRecord = objController.GetData("vw_ClinicalRecord", false, arrCoPatients, paramFullClinicalRecord);

            if (dtFullClinicalRecord != null && dtFullClinicalRecord.Rows.Count != 0)
            {
                DataRow drFullClinicalRecord = dtFullClinicalRecord.Rows[0];

                this.tbNgayKham.Visible = false;
                this.dtpNgayKham.Visible = true;
                this.clrNoiSoiISN = drFullClinicalRecord[InfoClinicalRecord.CLRISN].ToString();
                this.tbID.Text = drFullClinicalRecord[InfoPatients.PATID].ToString();
                this.tbFullName.Text = drFullClinicalRecord[InfoPatients.PATNAME].ToString();

                ///load lai thong tin TIEN CAN theo benh an duoc chon
                ///edit by thaoht 20100107
                loadThongTinTienCan(this.clrNoiSoiISN);

                //set image 4 patient --edit 20091017
                if (drFullClinicalRecord[InfoPatients.PATHASPIC].ToString() == "1")
                {
                    loadPic4Patient();
                }
                else
                {
                    this.picPatient.Image = null;
                }

                if (drFullClinicalRecord[InfoPatients.PATSEX].ToString() == "Nam")
                {
                    this.rbMale.Checked = true;
                }
                else
                {
                    this.rbFemale.Checked = true;
                }
                this.tbAddress.Text = drFullClinicalRecord[InfoPatients.PATADDRESS].ToString();
                this.tbPhone.Text = drFullClinicalRecord[InfoPatients.PATTEL].ToString();
                this.tbJob.Text = drFullClinicalRecord[InfoPatients.PATOCCUPATION].ToString();
                if (drFullClinicalRecord[InfoClinicalRecord.CLRPULSE].ToString() == "0")
                {
                    this.tbMach.Text = "";
                }
                else
                {
                    this.tbMach.Text = drFullClinicalRecord[InfoClinicalRecord.CLRPULSE].ToString();
                }
                if (drFullClinicalRecord[InfoClinicalRecord.CLRTEMPERATURE].ToString() == "0")
                {
                    this.tbNhietDo.Text = "";
                }
                else
                {
                    this.tbNhietDo.Text = drFullClinicalRecord[InfoClinicalRecord.CLRTEMPERATURE].ToString();
                }
                if (drFullClinicalRecord[InfoClinicalRecord.CLRBLOODPRESSURE].ToString() == "0")
                {
                    this.tbHuyetAp.Text = "";
                }
                else
                {
                    this.tbHuyetAp.Text = drFullClinicalRecord[InfoClinicalRecord.CLRBLOODPRESSURE].ToString();
                }
                if (drFullClinicalRecord[InfoClinicalRecord.CLRWEIGH].ToString() == "0")
                {
                    this.tbCanNang.Text = "";
                }
                else
                {
                    this.tbCanNang.Text = drFullClinicalRecord[InfoClinicalRecord.CLRWEIGH].ToString();
                }
                this.tbTienCan.Text = drFullClinicalRecord[InfoClinicalRecord.CLRMEDICALHISTORY].ToString();
                this.tbBenhNgoaiDS.Text = drFullClinicalRecord[InfoClinicalRecord.CLRSICKNOTINLIST].ToString();

                //edit here 20090606
                rbTMH.Checked = string.IsNullOrEmpty(drFullClinicalRecord[InfoClinicalRecord.CLRTYPE].ToString()) == true || drFullClinicalRecord[InfoClinicalRecord.CLRTYPE].ToString() == "1" ? true : false; //1 : TMH ; 2: TM
                rbTM.Checked = !rbTMH.Checked;

                this.tbThuThuat.Text = drFullClinicalRecord[InfoClinicalRecord.CLRSKILL].ToString();
                //edit here 20090822
                setDataSource4GridPhauThuat(drFullClinicalRecord[InfoClinicalRecord.CLRSURGERY].ToString(), drFullClinicalRecord[InfoClinicalRecord.CLRREXAMINATION4SURGERY].ToString());
                //neu có thủ thuật thì phẫu thuật sẽ không có. và người lại --edit 20090606
                if (string.IsNullOrEmpty(drFullClinicalRecord[InfoClinicalRecord.CLRSKILL].ToString().Trim()) == false)
                {
                    grpThuThuat.Selected = true;
                    grpPhauThuat.Selected = false;
                }
                else
                {
                    grpThuThuat.Selected = false;
                    grpPhauThuat.Selected = true;
                }

                this.tbLoiKhuyen.Text = drFullClinicalRecord[InfoClinicalRecord.CLRMEDICALADVICE].ToString();
                if (drFullClinicalRecord[InfoEmployees.EMPNAME].ToString().Trim() == "")
                {
                    this.cbBacSi.SelectedIndex = -1;
                    this.linkToaThuoc.Enabled = false;
                }
                else
                {
                    this.cbBacSi.SelectedIndex = this.cbBacSi.FindString(drFullClinicalRecord[InfoEmployees.EMPNAME].ToString());
                    this.linkToaThuoc.Enabled = true;
                }

                this.dtpBirthDay.Value = DateTime.Parse(drFullClinicalRecord[InfoPatients.PATBIRTHDATE].ToString());
                dtpBirthDay_Leave(sender, e);

                this.dtpNgayKham.Value = DateTime.Parse(drFullClinicalRecord[InfoClinicalRecord.CLREXAMINATIONDATE].ToString());
                this.dtpNgayTaiKham.Value = DateTime.Parse(drFullClinicalRecord[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE].ToString());

                this.ImagePath = drFullClinicalRecord[InfoClinicalRecord.CLRPICTURE].ToString();
                this.clrImageISN = drFullClinicalRecord[InfoClinicalRecord.CLRISN].ToString();
                this.tbDiDong.Text = drFullClinicalRecord[InfoPatients.PATMOBILE].ToString();
                this.tbBSu.Text = drFullClinicalRecord[InfoClinicalRecord.CLRSICKHISTORY].ToString();

                if (!string.IsNullOrEmpty(drFullClinicalRecord[InfoClinicalRecord.CLRAPPOINTMENTDATE].ToString()))
                {
                    this.dtpNgayHen.Value = DateTime.Parse(drFullClinicalRecord[InfoClinicalRecord.CLRAPPOINTMENTDATE].ToString());
                }
                this.tbDienBien.Text = drFullClinicalRecord[InfoClinicalRecord.CLRSICKCHANGE].ToString();

                List<ExpressionObject> paramClinicalSick = new List<ExpressionObject>();
                paramClinicalSick.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
                paramClinicalSick.Add(new ExpressionObject(InfoClinicalRecord.PATISN, this.patISN));
                DataTable dtClinicalSick = objController.GetData("vw_ClinicalSicks", paramClinicalSick);
                if (dtClinicalSick != null)
                {
                    this.listView1.Items.Clear();
                    for (int j = 0; j < dtClinicalSick.Rows.Count; j++)
                    {
                        ListViewItem item = new ListViewItem(dtClinicalSick.Rows[j][InfoSicks.SCKNAME].ToString());
                        switch (dtClinicalSick.Rows[j][InfoClinicalSick.SCKSICKLEVEL].ToString())
                        {
                            case "0":
                                item.ForeColor = Color.Red;
                                break;

                            case "1":
                                item.ForeColor = Color.Blue;
                                break;

                            case "2":
                                item.ForeColor = Color.Purple;
                                break;

                            case "3":
                                item.ForeColor = Color.Green;
                                break;

                            case "4":
                                item.ForeColor = Color.Gray;
                                break;
                        }
                        this.listView1.Items.AddRange(new ListViewItem[] { item });
                    }

                    List<ExpressionObject> paramPathologicalClinical = new List<ExpressionObject>();
                    paramPathologicalClinical.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
                    paramPathologicalClinical.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));
                    DataTable dtPathologicalClinical = objController.GetData("vw_PathologicalClinical", paramPathologicalClinical);

                    if (dtPathologicalClinical == null)
                    {
                        this.linkToaThuoc.Enabled = false;
                    }
                    else
                    {
                        this.lbCD.Items.Clear();
                        for (int k = 0; k < dtPathologicalClinical.Rows.Count; k++)
                        {
                            this.lbCD.Items.Add(dtPathologicalClinical.Rows[k][InfoPathologicalSigns.PLSPATHOLOGICALSIGNS].ToString());
                        }
                    }
                    this.tbCDNgoaiDS.Text = dtPathologicalClinical.Rows[0][InfoPathologicalOutList.POLPATHOLOGICALSIGNSOUTLIST].ToString();

                    this.insertXetNghiem = false;
                    this.linkXetNghiem.Enabled = true;
                    this.linkCTPhauThuat.Enabled = true; //edit here 20090606
                    this.dtpNgayHen.CustomFormat = "dd/MM/yyyy";
                    this.NgayKham = this.dtpNgayKham.Text.ToString();
                    if (!this.KTKhoiBenh())
                    {
                        this.chkHetBenh.Checked = true;
                        this.btCapNhat.Enabled = false;
                    }
                    else
                    {
                        this.chkHetBenh.Checked = false;
                        this.btCapNhat.Enabled = true;
                    }
                    ListDictionary paramCLRISN = new ListDictionary();
                    paramCLRISN.Add(InfoClinicalRecord.CLRISN, this.clrNoiSoiISN);
                    object iNLPISN = objController.Count(InfoAnalyzeResult.NLPISN, paramCLRISN, "vw_AnalyzeResult");

                    if (iNLPISN == null || (iNLPISN is int && (int)iNLPISN == 0))
                    {
                        this.linkXetNghiem.LinkColor = Color.Blue;
                    }
                    else
                    {
                        this.linkXetNghiem.LinkColor = Color.DarkGoldenrod;
                    }

                    object iCLRISN = objController.Count(InfoClinicalRecord.CLRISN, paramCLRISN, "vw_ScanResult_InnerScan");

                    if (iCLRISN == null || (iCLRISN is int && (int)iCLRISN == 0))
                    {
                        this.linkNoiSoi.LinkColor = Color.Blue;
                    }
                    else
                    {
                        this.linkNoiSoi.LinkColor = Color.DarkGoldenrod;
                    }

                    ///cần kiểm tra xem có CTPhauThuat khong???
                    ///nếu co thì tô màu khác---
                    iCLRISN = objController.Count(InfoClinicalRecord.CLRISN, paramCLRISN, InfoSurgeryDetail.TABLENAME);
                    if (iCLRISN == null || (iCLRISN is int && (int)iCLRISN == 0))
                    {
                        _blnInsertSurgeryDetail = true;
                        this.linkCTPhauThuat.LinkColor = Color.Blue;
                    }
                    else
                    {
                        _blnInsertSurgeryDetail = false;
                        this.linkCTPhauThuat.LinkColor = Color.DarkGoldenrod;
                    }
                }
            }
        }

        private void cboTaiKham_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ExpressionObject> param = new List<ExpressionObject>();
            param.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));
            param.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
            param.Add(new ExpressionObject(InfoClinicalRecord.CLRREXAMINATIONTIMES, this.cboTaiKham.Text));

            changeLanKham(param);
        }

        private void chbBAGoc_CheckedChanged(object sender, EventArgs e)
        {
            List<ExpressionObject> param = new List<ExpressionObject>();
            param.Add(new ExpressionObject(InfoPatients.PATISN, this.patISN));
            param.Add(new ExpressionObject(InfoClinicalRecord.CLREXAMINATIONTIMES, this.cbETimes.Text));
            if (this.chbBAGoc.Checked)
            {
                param.Add(new ExpressionObject(InfoClinicalRecord.CLRREXAMINATIONTIMES, 0));
                this.cboTaiKham.SelectedIndex = 0;
            }
            else if (!this.chbBAGoc.Checked)
            {
                int num = this.cboTaiKham.Items.Count - 1;
                param.Add(new ExpressionObject(InfoClinicalRecord.CLRREXAMINATIONTIMES, num));
                this.cboTaiKham.SelectedIndex = num;
            }
            changeLanKham(param);
        }

        private void chkHetBenh_Click(object sender, EventArgs e)
        {
            if (this.chkHetBenh.Checked)
            {
                if (MessageBox.Show("Bệnh nhân này đã khỏi bệnh?", "Tình Trạng Bệnh", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.chkHetBenh.Checked = true;
                }
                else
                {
                    this.chkHetBenh.Checked = false;
                }
            }
        }

        private void dtpBirthDay_Leave(object sender, EventArgs e)
        {
            this.tbTuoi.Text = (Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(dtpBirthDay.Value.Year)).ToString();
        }

        private void lbCD_DoubleClick(object sender, EventArgs e)
        {
            this.lbCD.Items.Remove(this.lbCD.SelectedItem);
        }

        private void lbCD_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.toolTip1.SetToolTip(this.lbCD, this.lbCD.Text.ToString());
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = this.listView1.SelectedItems[0];
                this.listView1.Items.Remove(item);
            }
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                frmBangMau mau = new frmBangMau();
                mau.ShowInTaskbar = false;
                mau.ShowDialog();
                if (this.listView1.SelectedItems.Count > 0)
                {
                    ListViewItem item = this.listView1.SelectedItems[0];
                    ListViewItem item2 = new ListViewItem(item.Text.ToString());
                    if (Class.mau == "Red")
                    {
                        item2.ForeColor = Color.Red;
                    }
                    if (Class.mau == "Green")
                    {
                        item2.ForeColor = Color.Green;
                    }
                    if (Class.mau == "Purple")
                    {
                        item2.ForeColor = Color.Purple;
                    }
                    if (Class.mau == "Blue")
                    {
                        item2.ForeColor = Color.Blue;
                    }
                    if (Class.mau == "Gray")
                    {
                        item2.ForeColor = Color.Gray;
                    }
                    this.listView1.Items.AddRange(new ListViewItem[] { item2 });
                    this.listView1.Items.Remove(item);
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = this.listView1.SelectedItems[0];
                this.toolTip1.SetToolTip(this.listView1, item.Text.ToString());
            }
        }

        private void tbDiDong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && (e.KeyChar != '\b'))
            {
                e.Handled = true;
            }
        }

        private void tbMach_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ',')
            {
                e.Handled = true;
            }
        }

        private void tbPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && (e.KeyChar != '\b'))
            {
                e.Handled = true;
            }
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string str = this.treeView1.SelectedNode.Text.ToString();
                int count = 0;
                int num2 = 0;
                if (!this.treeView1.SelectedNode.Parent.IsSelected)
                {
                    count = this.listView1.Items.Count;
                    if (count == 0)
                    {
                        ListViewItem item = new ListViewItem(this.treeView1.SelectedNode.Text.ToString());
                        item.ForeColor = Color.Gray;
                        this.listView1.Items.AddRange(new ListViewItem[] { item });
                    }
                    else
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (str == this.listView1.Items[i].Text.ToString())
                            {
                                num2++;
                            }
                        }
                        if (num2 == 0)
                        {
                            ListViewItem item2 = new ListViewItem(this.treeView1.SelectedNode.Text.ToString());
                            item2.ForeColor = Color.Gray;
                            this.listView1.Items.AddRange(new ListViewItem[] { item2 });
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private void treeView2_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string str = this.treeView2.SelectedNode.Text.ToString();
                int count = 0;
                int num2 = 0;
                if (!this.treeView2.SelectedNode.Parent.IsSelected)
                {
                    count = this.lbCD.Items.Count;
                    if (count == 0)
                    {
                        this.lbCD.Items.Add(this.treeView2.SelectedNode.Text.ToString());
                    }
                    else
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (str == this.lbCD.Items[i].ToString())
                            {
                                num2++;
                            }
                        }
                        if (num2 == 0)
                        {
                            this.lbCD.Items.Add(this.treeView2.SelectedNode.Text.ToString());
                        }
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// edit here 20090606
        /// nếu dã có phẫu thuật thì không có thủ thuật
        /// và ngược lại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grpPhauThuat_DoubleClick(object sender, EventArgs e)
        {
            grpThuThuat.Selected = false;
        }

        /// <summary>
        /// edit here 20090606
        /// nếu dã có phẫu thuật thì không có thủ thuật
        /// và ngược lại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grpThuThuat_DoubleClick(object sender, EventArgs e)
        {
            grpPhauThuat.Selected = false;
        }

        /// <summary>
        /// edit here  20090606
        /// nếu dã có phẫu thuật thì không có thủ thuật
        /// và ngược lại
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbTMH_CheckedChanged(object sender, EventArgs e)
        {
            if (rbTMH.Checked == true)
            {
                grpPhauThuat.Selected = false;
                grpThuThuat.Selected = true;
            }
            else
            {
                grpPhauThuat.Selected = true;
                grpThuThuat.Selected = false;
            }

        }

        #region Link Event
        /// <summary>
        /// xem nội dung chi tiết phẫu thuật
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkCTPhauThuat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (gdPT.DataSource == null || (DataTable)gdPT.DataSource == null || ((DataTable)gdPT.DataSource).Rows.Count == 0)
            {
                MessageBox.Show("Bạn vui lòng chọn thông tin phẫu thuật", "Thông Báo", MessageBoxButtons.OK);
                return;
            }
            if ((this.clrNoiSoiISN != string.Empty) && (this.clrNoiSoiISN != ""))
            {
                string strTK4PT = string.Empty;
                string strPT = getStringPhauThuat(ref strTK4PT);
                string strLanTaiKham = string.Empty;
                string strLanKham = string.Empty;
                DateTime dteNgayKham;
                if (cbETimes.Visible == false)
                {
                    strLanKham = tbLanKham.Text;
                }
                else
                {
                    strLanKham = cbETimes.SelectedItem.ToString();
                }
                if (cboTaiKham.Visible == false)
                {
                    strLanTaiKham = txtTaiKham.Text;
                }
                else
                {
                    strLanTaiKham = cboTaiKham.SelectedItem.ToString();
                }
                if (dtpNgayKham.Visible == false)
                {
                    dteNgayKham = DateTime.Now;
                }
                else
                {
                    dteNgayKham = dtpNgayKham.Value;
                }
                frmCTPhauThuat ctpt = new frmCTPhauThuat(this.patISN, this.tbID.Text, this.clrNoiSoiISN, (DataTable)cbBacSi.DataSource, _blnInsertSurgeryDetail, strLanKham, strLanTaiKham, strPT, dteNgayKham);
                ctpt.ShowInTaskbar = false;
                ctpt.ShowDialog(this);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if ((this.clrNoiSoiISN != string.Empty) && (this.clrNoiSoiISN != ""))
            {
                //phan them moi --edit by thaoht 20090531
                Hashtable _hashParam = new Hashtable();
                _hashParam[InfoEmployees.EMPID] = this.cbBacSi.SelectedValue;
                _hashParam[InfoEmployees.EMPNAME] = this.cbBacSi.Text;
                _hashParam[InfoPatients.PATID] = this.tbID.Text;
                _hashParam[InfoPatients.PATNAME] = this.tbFullName.Text;
                _hashParam[InfoPatients.PATSEX] = rbMale.Checked == true ? rbMale.Text : rbFemale.Text;
                _hashParam[InfoPatients.PATAGE] = tbTuoi.Text;
                _hashParam[InfoPatients.PATADDRESS] = tbAddress.Text;
                _hashParam[InfoClinicalRecord.CLRMEDICALADVICE] = tbLoiKhuyen.Text;
                _hashParam[InfoClinicalRecord.CLRSKILL] = tbThuThuat.Text;

                string strCLRPATHOLOGICALSIGNS = string.Empty;
                if (rbTMH.Checked == true)
                {
                    for (int k = 0; k < lbCD.Items.Count; k++)
                    {
                        _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] += this.lbCD.Items[k].ToString() + ",";
                    }
                    if (_hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] != null)
                    {
                        _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] = string.IsNullOrEmpty(this.tbCDNgoaiDS.Text.Trim()) == true ? _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].ToString() + this.tbCDNgoaiDS.Text.Trim() : _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].ToString().Remove(_hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].ToString().Length - 1, 1);
                    }
                    else
                    {
                        _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] = this.tbCDNgoaiDS.Text.Trim();
                    }
                }
                else //edit here 20090822 : neu la TM thi CD = PT + CDNDS
                {
                    string strTK = string.Empty;
                    strCLRPATHOLOGICALSIGNS = "- " + getStringPhauThuat(ref strTK);
                    strCLRPATHOLOGICALSIGNS += string.IsNullOrEmpty(this.tbCDNgoaiDS.Text.Trim()) == true ? string.Empty : this.tbCDNgoaiDS.Text.Trim();
                    _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS] = strCLRPATHOLOGICALSIGNS;
                }

                _hashParam[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE] = dtpNgayTaiKham.Text;
                //end phan them moi --edit by thaoht 20090531

                DataTable dtEmployee = (DataTable)cbBacSi.DataSource;
                dtEmployee.PrimaryKey = new DataColumn[] { dtEmployee.Columns[InfoEmployees.EMPISN] };
                byte[] byteEmpSignature = null;
                if (dtEmployee.Rows.Find(cbBacSi.SelectedValue)[InfoEmployees.EMPSIGNATUREIMAGE] is byte[])
                    byteEmpSignature = (byte[])dtEmployee.Rows.Find(cbBacSi.SelectedValue)[InfoEmployees.EMPSIGNATUREIMAGE];

                ListDictionary param = new ListDictionary();
                param.Add(InfoMedicineExport.CLRISN, this.clrNoiSoiISN);
                object iMESISN = objController.Count(InfoMedicineExport.MESISN, param, InfoMedicineExport.TABLENAME);

                if (iMESISN == null || (int)iMESISN == 0)
                {
                    int num = 0;
                    int num2 = 0;
                    if (this.cbETimes.Visible && !this.cboTaiKham.Visible)
                    {
                        num = int.Parse(this.cbETimes.Text);
                        num2 = 0;
                    }
                    else if (!this.cbETimes.Visible && !this.cboTaiKham.Visible)
                    {
                        if ((int.Parse(this.txtTaiKham.Text) == 0) && (int.Parse(this.tbLanKham.Text) > 1))
                        {
                            num = int.Parse(this.tbLanKham.Text);
                            num2 = 0;
                        }
                        else
                        {
                            num = int.Parse(this.tbLanKham.Text);
                            num2 = int.Parse(this.txtTaiKham.Text);
                        }
                    }
                    else if (this.cbETimes.Visible && this.cboTaiKham.Visible)
                    {
                        num = int.Parse(this.cbETimes.Text);
                        num2 = int.Parse(this.cboTaiKham.Text);
                    }

                    DataTable dtMaxPrescription = objController.Getfn_MaxRexamTimesOFMaxExamTimesNoJoinMedExport(this.tbID.Text, num.ToString(), num2.ToString());

                    string strOldClrISN = dtMaxPrescription.Rows[0][InfoClinicalRecord.OLDCLRISN].ToString(); //edit here 20090531 mã bệnh án cho lần khám có examtimes và reExamTime gần nhất.
                    frmToaThuocHoSoBenhAn an = new frmToaThuocHoSoBenhAn(this.clrNoiSoiISN, this.tbID.Text.ToString(), this.tbFullName.Text.ToString(), strOldClrISN, _hashParam, byteEmpSignature);
                    an.ShowInTaskbar = false;
                    an.ShowDialog(this);
                }
                else
                {
                    this.ID = this.tbID.Text;
                    this.Ten = this.tbFullName.Text;

                    frmXemToaThuoc thuoc = new frmXemToaThuoc(this, _hashParam, byteEmpSignature);
                    thuoc.ShowInTaskbar = false;
                    thuoc.ShowDialog(this);
                }
            }
        }

        private void linkNoiSoi_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmNoiSoi soi = new frmNoiSoi(this.tbID.Text.ToString(), this.tbFullName.Text.ToString(), this.clrNoiSoiISN, this.Insert, this.NgayKham);
            soi.ShowInTaskbar = false;
            soi.ShowDialog();
        }

        private void linkXetNghiem_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if ((this.clrNoiSoiISN != string.Empty) && (this.clrNoiSoiISN != ""))
            {
                bool flag;

                ListDictionary param = new ListDictionary();
                param.Add(InfoAnalyzeResult.CLRISN, this.clrNoiSoiISN);
                object iNLPISN = objController.Count(InfoAnalyzeResult.NLPISN, param, "vw_AnalyzeResult");

                if (iNLPISN == null || (int)iNLPISN == 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                flag = true;
                frmKQXetNghiem nghiem = new frmKQXetNghiem(this.tbID.Text.ToString(), this.tbFullName.Text.ToString(), this.clrNoiSoiISN, flag);
                nghiem.ShowInTaskbar = false;
                nghiem.ShowDialog();
            }
        }
        #endregion

        private void picPatient_ImageChanged(object sender, EventArgs e)
        {
            _blnChangeImage = true;
        }

        #region TienCan

        private void loadThongTinTienCan(string _strClrISN)
        {
            this.ucTienCan.PatientID = this.patISN;
            string surISN = "-9999"; // 4 Tien Can //20091216
            this.ucTienCan.SurgicalID = surISN;
            this.ucTienCan.ExtraID1 = _strClrISN;
            this.ucTienCan.getData();
        }
        #endregion

        #endregion event


    }
}